美文网首页iOS 学习
swift 设置部分圆角

swift 设置部分圆角

作者: 零粹 | 来源:发表于2019-01-18 14:37 被阅读0次

    可以将下面的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 设置部分圆角

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