locked
How can i create borders in label? RRS feed

  • Question

  • User145757 posted

    Hi, i'm new in IOS and i'm trying make edges to label.

    How can i do it? i can't find any documentation about it.

    can i do it with storyborad?

    Monday, August 24, 2015 10:37 AM

Answers

  • User7747 posted

    You can also do it with layers, which makes it a lot easier to deal with and reuse.

    Here's a simple example for a UITextField, but the same idea would apply to a UILabel:

        [Flags]
        public enum UITextFieldBorder
        {
            None = 0,
            Left = 1,
            Top = 2,
            Right = 4,
            Bottom = 8,
            All = 15
        }
    
        [Register("BorderedTextField")]
        public class BorderedTextField: UITextField
        {
            private CALayer bottom;
            private CALayer right;
            private CALayer top;
            private CALayer left;
            private UITextFieldBorder borderMask;
    
            public BorderedFLTextField(IntPtr handle)
                : base(handle)
            {
                CommonInit();
            }
    
            public BorderedFLTextField(CGRect frame)
                : base(frame)
            {
                CommonInit();
            }
    
            public BorderedFLTextField(NSCoder coder)
                : base(coder)
            {
                CommonInit();
            }
    
            #endregion
    
            private void CommonInit()
            {
                ContentMode = UIViewContentMode.Redraw;
            }
    
            public UITextFieldBorder BorderMask
            {
                get { return borderMask; }
                set
                {
    
                    borderMask = value;
    
                    if (top != null)
                    {
                        top.RemoveFromSuperLayer();
                        top.Dispose();
                    }
                    if (bottom != null)
                    {
                        bottom.RemoveFromSuperLayer();
                        bottom.Dispose();
                    }
                    if (left != null)
                    {
                        left.RemoveFromSuperLayer();
                        left.Dispose();
                    }
                    if (right != null)
                    {
                        right.RemoveFromSuperLayer();
                        right.Dispose();
                    }
    
                    if ((borderMask & UITextFieldBorder.Bottom) != UITextFieldBorder.None)
                        bottom = BuildLayer();
    
                    if ((borderMask & UITextFieldBorder.Top) != UITextFieldBorder.None)
                        top = BuildLayer();
    
                    if ((borderMask & UITextFieldBorder.Left) != UITextFieldBorder.None)
                        left = BuildLayer();
    
                    if ((borderMask & UITextFieldBorder.Right) != UITextFieldBorder.None)
                        right = BuildLayer();
    
                    Layer.MasksToBounds = true;
                    SetNeedsLayout();
                }
            }
    
            private CALayer BuildLayer()
            {
                var layer = new CALayer()
                {
                    BorderColor = UIColor.FromRGB(199f / 255f, 199f / 255f, 204f / 255f).CGColor,
                    Frame = CGRect.Empty,
                    BorderWidth = 1f
                };
    
                Layer.AddSublayer(layer);
                return layer;
            }
    
            public override void LayoutSubviews()
            {
                base.LayoutSubviews();
    
                if (bottom != null)
                    bottom.Frame = new CGRect(0, Layer.Bounds.Height - 1, Layer.Bounds.Width, 1);
    
                if (right != null)
                    right.Frame = new CGRect(Layer.Bounds.Width - 1, 0, 1, Layer.Bounds.Height);
    
                if (left != null)
                    left.Frame = new CGRect(0, 0, 1, Layer.Bounds.Height);
    
                if (top != null)
                    top.Frame = new CGRect(0, 0, Layer.Bounds.Width, 1); 
            }
    
            protected override void Dispose(bool disposing)
            {
                base.Dispose(disposing);
    
                if (disposing)
                {
                    if (top != null)
                    {
                        top.RemoveFromSuperLayer();
                        top.Dispose();
                    }
                    if (bottom != null)
                    {
                        bottom.RemoveFromSuperLayer();
                        bottom.Dispose();
                    }
                    if (left != null)
                    {
                        left.RemoveFromSuperLayer();
                        left.Dispose();
                    }
                    if (right != null)
                    {
                        right.RemoveFromSuperLayer();
                        right.Dispose();
                    }
                }
            }
        }
    
    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Monday, August 24, 2015 2:01 PM

All replies

  • User145757 posted

    I just do it with a UIView :)

    Monday, August 24, 2015 10:56 AM
  • User7747 posted

    You can also do it with layers, which makes it a lot easier to deal with and reuse.

    Here's a simple example for a UITextField, but the same idea would apply to a UILabel:

        [Flags]
        public enum UITextFieldBorder
        {
            None = 0,
            Left = 1,
            Top = 2,
            Right = 4,
            Bottom = 8,
            All = 15
        }
    
        [Register("BorderedTextField")]
        public class BorderedTextField: UITextField
        {
            private CALayer bottom;
            private CALayer right;
            private CALayer top;
            private CALayer left;
            private UITextFieldBorder borderMask;
    
            public BorderedFLTextField(IntPtr handle)
                : base(handle)
            {
                CommonInit();
            }
    
            public BorderedFLTextField(CGRect frame)
                : base(frame)
            {
                CommonInit();
            }
    
            public BorderedFLTextField(NSCoder coder)
                : base(coder)
            {
                CommonInit();
            }
    
            #endregion
    
            private void CommonInit()
            {
                ContentMode = UIViewContentMode.Redraw;
            }
    
            public UITextFieldBorder BorderMask
            {
                get { return borderMask; }
                set
                {
    
                    borderMask = value;
    
                    if (top != null)
                    {
                        top.RemoveFromSuperLayer();
                        top.Dispose();
                    }
                    if (bottom != null)
                    {
                        bottom.RemoveFromSuperLayer();
                        bottom.Dispose();
                    }
                    if (left != null)
                    {
                        left.RemoveFromSuperLayer();
                        left.Dispose();
                    }
                    if (right != null)
                    {
                        right.RemoveFromSuperLayer();
                        right.Dispose();
                    }
    
                    if ((borderMask & UITextFieldBorder.Bottom) != UITextFieldBorder.None)
                        bottom = BuildLayer();
    
                    if ((borderMask & UITextFieldBorder.Top) != UITextFieldBorder.None)
                        top = BuildLayer();
    
                    if ((borderMask & UITextFieldBorder.Left) != UITextFieldBorder.None)
                        left = BuildLayer();
    
                    if ((borderMask & UITextFieldBorder.Right) != UITextFieldBorder.None)
                        right = BuildLayer();
    
                    Layer.MasksToBounds = true;
                    SetNeedsLayout();
                }
            }
    
            private CALayer BuildLayer()
            {
                var layer = new CALayer()
                {
                    BorderColor = UIColor.FromRGB(199f / 255f, 199f / 255f, 204f / 255f).CGColor,
                    Frame = CGRect.Empty,
                    BorderWidth = 1f
                };
    
                Layer.AddSublayer(layer);
                return layer;
            }
    
            public override void LayoutSubviews()
            {
                base.LayoutSubviews();
    
                if (bottom != null)
                    bottom.Frame = new CGRect(0, Layer.Bounds.Height - 1, Layer.Bounds.Width, 1);
    
                if (right != null)
                    right.Frame = new CGRect(Layer.Bounds.Width - 1, 0, 1, Layer.Bounds.Height);
    
                if (left != null)
                    left.Frame = new CGRect(0, 0, 1, Layer.Bounds.Height);
    
                if (top != null)
                    top.Frame = new CGRect(0, 0, Layer.Bounds.Width, 1); 
            }
    
            protected override void Dispose(bool disposing)
            {
                base.Dispose(disposing);
    
                if (disposing)
                {
                    if (top != null)
                    {
                        top.RemoveFromSuperLayer();
                        top.Dispose();
                    }
                    if (bottom != null)
                    {
                        bottom.RemoveFromSuperLayer();
                        bottom.Dispose();
                    }
                    if (left != null)
                    {
                        left.RemoveFromSuperLayer();
                        left.Dispose();
                    }
                    if (right != null)
                    {
                        right.RemoveFromSuperLayer();
                        right.Dispose();
                    }
                }
            }
        }
    
    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Monday, August 24, 2015 2:01 PM