Xamarin.Forms - can I add a label add-on with a custom renderer? (IOS)

Is there a way to add a label add-on with a custom renderer? I know that you can cheat by adding a content view around the label and adding an addition to the content view; but I want the user interface to be cleaner and there is no need to add an additional element.

Just to be clear, I don't want a margin - in other words, if I add a background color to the label, you should see the indentation between the text and the label background, for example:

enter image description here

+4
source share
1 answer

Have you tried something like this:

namespace CustomFinder.iOS.Renderers
{   
    public class DataLabelRenderer : LabelRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<Label> e)
        {
            if (Control == null)
            {
                SetNativeControl(new TagUiLabel());
            }

            base.OnElementChanged(e);
        }
    }

    public class TagUiLabel : UILabel
    {
        private UIEdgeInsets EdgeInsets { get; set; }

        public TagUiLabel()
        {
            EdgeInsets = new UIEdgeInsets(0, 3, 0, 3);
        }
        public override void DrawText(CoreGraphics.CGRect rect)
        {
            base.DrawText(EdgeInsets.InsetRect(rect));
        }
    }
}

.

+2

All Articles