Resizing a UILabel to accommodate insets

后端 未结 8 1739
梦谈多话
梦谈多话 2020-11-27 11:03

I\'m building a screen to scan barcodes, and I need to put a translucent screen behind some UILabels to improve visibility against light backgrounds.

He

8条回答
  •  孤独总比滥情好
    2020-11-27 11:06

    Here is the C# version (usefull for Xamarin) based on Nikolai's code :

    public class UIEdgeableLabel : UILabel
    {
        public UIEdgeableLabel() : base() { }
        public UIEdgeableLabel(NSCoder coder) : base(coder) { }
        public UIEdgeableLabel(CGRect frame) : base(frame) { }
        protected UIEdgeableLabel(NSObjectFlag t) : base(t) { }
    
        private UIEdgeInsets _edgeInset = UIEdgeInsets.Zero;
        public UIEdgeInsets EdgeInsets
        {
            get { return _edgeInset; }
            set
            {
                _edgeInset = value;
                this.InvalidateIntrinsicContentSize();
            }
        }
    
        public override CGRect TextRectForBounds(CGRect bounds, nint numberOfLines)
        {
            var rect = base.TextRectForBounds(EdgeInsets.InsetRect(bounds), numberOfLines);
            return new CGRect(x: rect.X - EdgeInsets.Left,
                              y: rect.Y - EdgeInsets.Top,
                              width: rect.Width + EdgeInsets.Left + EdgeInsets.Right,
                              height: rect.Height + EdgeInsets.Top + EdgeInsets.Bottom);
        }
    
        public override void DrawText(CGRect rect)
        {
            base.DrawText(this.EdgeInsets.InsetRect(rect));
        }
    }
    

提交回复
热议问题