swift label only border left

前端 未结 6 1261
[愿得一人]
[愿得一人] 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条回答
  •  闹比i
    闹比i (楼主)
    2021-02-03 13:06

    Here is an extension you can add to your project:

    extension CALayer {
    
        func addBorder(edge: UIRectEdge, color: UIColor, thickness: CGFloat) {
    
            var border = CALayer()
    
            switch edge {
            case UIRectEdge.Top:
                border.frame = CGRectMake(0, 0, CGRectGetHeight(self.frame), thickness)
                break
            case UIRectEdge.Bottom:
                border.frame = CGRectMake(0, CGRectGetHeight(self.frame) - thickness, UIScreen.mainScreen().bounds.width, thickness)
                break
            case UIRectEdge.Left:
                border.frame = CGRectMake(0, 0, thickness, CGRectGetHeight(self.frame))
                break
            case UIRectEdge.Right:
                border.frame = CGRectMake(CGRectGetWidth(self.frame) - thickness, 0, thickness, CGRectGetHeight(self.frame))
                break
            default:
                break
            }
    
            border.backgroundColor = color.CGColor;
    
            self.addSublayer(border)
        }
    
    }
    

    And the use it like this:

    cell.Label.layer.addBorder(UIRectEdge.Top, color: UIColor.greenColor(), thickness: 0.5)
    

    FOR SWIFT 3, 4 & 5:

    
    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.height, height: thickness)
                break
            case UIRectEdge.bottom:
                border.frame = CGRect(x: 0, y: self.frame.height - thickness, width: UIScreen.main.bounds.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:
                break
            }
    
            border.backgroundColor = color.cgColor;
    
            self.addSublayer(border)
        }
    
    }
    

提交回复
热议问题