4

Is there a way to add padding to a label using a custom renderer? I know you can cheat by adding a content view around the label and adding padding to the content view; but I want to keep the UI cleaner and not have to add an extra 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 padding between the text and the background of the label, like this:

enter image description here


  • I could not find anything using styles or custom rendering. I know you dont want to con volute your UI but what about a custom control with a text field overlapping some background. In essence it is what you were avoiding but without the messy code in your UI. - Clint Landry

1 답변


3

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));
        }
    }
}

I have this from here haven't tried it yet.


Latest