swift label only border left

前端 未结 6 1324
[愿得一人]
[愿得一人] 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:44

    Apart from using an extension, you can also use @IBDesignable / @IBInspectable to create a subclass from UILabel and draw the border there. This has some advantages, because you can then use the Interface Builder to style your label and it is shown directly in the Storyboard.

    @IBDesignable
    class LeftBorderedLabel: UILabel {
    
        @IBInspectable var blockColor: UIColor = UIColor.black {
    
            didSet{
    
                let border = CALayer()
                border.frame = CGRect(x: 0, y: 0, width: 15, height: self.frame.height)
                border.backgroundColor = blockColor.cgColor;
    
                self.layer.addSublayer(border)
            }
        }
    
        override func prepareForInterfaceBuilder() {
    
            super.prepareForInterfaceBuilder()
        }
    }
    

    Example in interface builder (example is for a tableViewCell):

提交回复
热议问题