0

I'm adding padding to UILabel

public partial class MessageLabel : UILabel
    {
        public MessageLabel(IntPtr handle) : base(handle)
        {
        }

        public override void DrawText(CoreGraphics.CGRect rect)
        {
            UIEdgeInsets insets = new UIEdgeInsets() { Bottom = 5, Top = 5, Left = 5, Right = 5 };
            base.DrawText(insets.InsetRect(rect));
        }
    }

Padding are added well, but last line is cropped, due to extra empty space I added... If I increate height with 10 in this methods, padding are added only at the top of label.

with paddings screen 1 - looks nice, but missing words: enter image description here

without passings screen 2 - show whole message: enter image description here

Any solution?

Nininea
  • 2,671
  • 6
  • 31
  • 57
  • Are you using numberOfLines = 0? – Daniel Kuta Sep 26 '17 at 08:42
  • @DanielQ yes, I do – Nininea Sep 26 '17 at 08:49
  • the solution provided by @DanielQ works on iOS, but there is something wrong with xamarin.ios, I got the same test result as you described in question, to fit this problem, maybe you should set height manually according to the text content. `CGSize size = label.Text.StringSize(label.Font, new CGSize(widthConstraint.Constant - 50, 9999),UILineBreakMode.WordWrap);heightConstraint.Constant = size.Height;` – ColeX Sep 27 '17 at 08:11

1 Answers1

0

Try this:

    private var topInset: CGFloat = 5
    private var bottomInset: CGFloat = 5
    private var leftInset: CGFloat = 5
    private var rightInset: CGFloat = 5

    override func drawTextInRect(rect: CGRect) {

      let insets = UIEdgeInsets(top: topInset, left: leftInset, bottom: bottomInset, right: rightInset)
          super.drawTextInRect(UIEdgeInsetsInsetRect(rect, insets))
      }

override public var intrinsicContentSize: CGSize {
    var intrinsicSuperViewContentSize = super.intrinsicContentSize
    intrinsicSuperViewContentSize.height += topInset + bottomInset
    intrinsicSuperViewContentSize.width += leftInset + rightInset
    return intrinsicSuperViewContentSize
}
Daniel Kuta
  • 1,634
  • 15
  • 24