swift 设置部分圆角

可以将下面的configRectCorner方法写成公共方法,方便调用

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: [.topRight, .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
   }

demo https://github.com/ITHanYong/HYRectCorner-swift.git

你可能感兴趣的:(swift 设置部分圆角)