美文网首页程序员
Swift添加部分圆角

Swift添加部分圆角

作者: 心猿意码_ | 来源:发表于2022-06-24 13:51 被阅读0次
    let label = UILabel(frame: CGRect(x: 50, y: 100, width: 100, height: 50))
    label.backgroundColor = UIColor.orange
    label.textColor = UIColor.white
    label.text = "圆角设置"
    label.textAlignment = .center
           
    label.layer.mask = self.configRectCorner(view: label, corner: [topLeft, .topRight,  bottomLeft, .bottomRight], radii: CGSize(width: 15, height: 15))
           
    self.view.addSubview(label)
       
       /// 圆角设置
       ///
       /// - Parameters:
       ///   - view: 需要设置的控件
       ///   - corner: 哪些圆角
       ///   - radii: 圆角半径
       /// - Returns: layer图层
       func configRectCorner(view: UIView, corner: UIRectCorner, radii: CGSize) -> CALayer {
           
           let maskPath = UIBezierPath.init(roundedRect: view.bounds, byRoundingCorners: corner, cornerRadii: radii)
           
           let maskLayer = CAShapeLayer.init()
           maskLayer.frame = view.bounds
           maskLayer.path = maskPath.cgPath
           
           return maskLayer
       }
    

    相关文章

      网友评论

        本文标题:Swift添加部分圆角

        本文链接:https://www.haomeiwen.com/subject/zogvvrtx.html