1
myLabel.layer.borderWidth = 0.5
myLabel.layer.borderColor = UIColor.green.cgColor

Thats a simple answer of drawing a border. However the issue is I need to draw the border only to the right and bottom. I also need another label to draw border on top and right. How can I draw border to specific sides, not all 4 sides?

krikor Herlopian
  • 731
  • 1
  • 10
  • 23
  • Possible duplicate of [CALayer: add a border only at one side](http://stackoverflow.com/questions/7022656/calayer-add-a-border-only-at-one-side) – Andreas Oetjen Feb 10 '17 at 16:08

2 Answers2

1

Add a shape layer and draw the lines in that layer. Note that layers do not participate in auto layout so you need to put your code in viewDidLayoutSubviews or subclass uilabel and do this in layout subviews. Here is a playground example using the UILabel subclass:

    import PlaygroundSupport
    import UIKit
    class L: UILabel {
        var strokeColor = UIColor.blue
        var strokeWidth = CGFloat(0.5)
        private lazy var labelBorderLayer:CAShapeLayer = {
            let shapeLayer = CAShapeLayer()
            self.layer.addSublayer(shapeLayer)
            return shapeLayer
        }()
        override func layoutSubviews() {
            super.layoutSubviews()
            let path = CGMutablePath()
            path.move(to: CGPoint(x: 0, y: bounds.size.height))
            path.addLine(to: CGPoint(x:  bounds.size.width, y: bounds.size.height))
            path.addLine(to: CGPoint(x:  bounds.size.width, y: 0))
            labelBorderLayer.path = path
            labelBorderLayer.strokeColor = strokeColor.cgColor
            labelBorderLayer.lineWidth = strokeWidth
            labelBorderLayer.fillColor = UIColor.clear.cgColor
        }
    }

    let v = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
    let l = L(frame: v.frame.insetBy(dx: 50, dy: 80))
    v.addSubview(l)
    l.textColor = .white
    l.textAlignment = .center
    l.text = "gjgkjgjgjgj"
    v.backgroundColor = .red
    PlaygroundPage.current.liveView = v
Josh Homann
  • 15,933
  • 3
  • 30
  • 33
0

Im not sure if you can do that with the actual label border. But but you can do something similar to this

    public enum UIButtonBorderSide {
       case Top, Bottom, Left, Right
    }

extension UIButton {   
public func addBorder(side: UIButtonBorderSide, color: UIColor, width: CGFloat) {
    let border = CALayer()
    border.backgroundColor = color.CGColor

    switch side {
    case .Top:
        border.frame = CGRect(x: 0, y: 0, width: frame.size.width, height: width)
    case .Bottom:
        border.frame = CGRect(x: 0, y: self.frame.size.height - width, width: self.frame.size.width, height: width)
    case .Left:
        border.frame = CGRect(x: 0, y: 0, width: width, height: self.frame.size.height)
    case .Right:
        border.frame = CGRect(x: self.frame.size.width - width, y: 0, width: width, height: self.frame.size.height)
    }

    self.layer.addSublayer(border)
}
}

Thats for UIButton, But you can probably adapt it easily.

Source: https://gist.github.com/Isuru-Nanayakkara/496d5713e61125bddcf5

Hope this helps

toiavalle
  • 414
  • 7
  • 20