swift label only border left

前端 未结 6 1260
[愿得一人]
[愿得一人] 2021-02-03 12:13

good morning together,

i have a tableview like this:

Example: in cell one i have got an red text label on the right side. left from it i include an image like a

6条回答
  •  臣服心动
    2021-02-03 12:56

    Here is an extension you can add to your project.

    SWIFT 3 :-

    extension CALayer {
    
        func addBorder(edge: UIRectEdge, color: UIColor, thickness: CGFloat) {
    
            let border = CALayer()
    
            switch edge {
            case UIRectEdge.top:
                border.frame = CGRect(x: 0, y: 0, width: self.frame.width, height: thickness)
                break
            case UIRectEdge.bottom:
                border.frame = CGRect(x: 0, y: self.frame.height - thickness, width: self.frame.width, height: thickness)
                break
            case UIRectEdge.left:
                border.frame = CGRect(x: 0, y: 0, width: thickness, height: self.frame.height)
                break
            case UIRectEdge.right:
                border.frame = CGRect(x: self.frame.width - thickness, y: 0, width: thickness, height: self.frame.height)
                break
            default:
                //For Center Line
                border.frame = CGRect(x: self.frame.width/2 - thickness, y: 0, width: thickness, height: self.frame.height)
                break
            }
    
            border.backgroundColor = color.cgColor;
            self.addSublayer(border)
        }
    }
    

    And the use it like this:

    labelName.layer.addBorder(edge: UIRectEdge.right, color: UIColor.black, thickness: 1.5)
    

    If you want to draw a line on Center, then you can set following frame in any of the scenario :-

    border.frame = CGRect(x: self.frame.width/2 - thickness, y: 0, width: thickness, height: self.frame.height)
    

提交回复
热议问题