how to set cornerRadius for only bottom-left,bottom-right and top-left corner textview?

前端 未结 12 824
名媛妹妹
名媛妹妹 2020-11-27 12:50

How to set corner radius only only bottom-left,bottom-right and top-left corner textview?

let rectShape = CAShapeLayer()
    rectShape.backgroundColor = UICo         


        
相关标签:
12条回答
  • 2020-11-27 13:14
    extension UIView {
    
    func roundCorners(_ corners: CACornerMask, radius: CGFloat) {
        if #available(iOS 11, *) {
            self.layer.cornerRadius = radius
            self.layer.maskedCorners = corners
        } else {
            var cornerMask = UIRectCorner()
            if(corners.contains(.layerMinXMinYCorner)){
                cornerMask.insert(.topLeft)
            }
            if(corners.contains(.layerMaxXMinYCorner)){
                cornerMask.insert(.topRight)
            }
            if(corners.contains(.layerMinXMaxYCorner)){
                cornerMask.insert(.bottomLeft)
            }
            if(corners.contains(.layerMaxXMaxYCorner)){
                cornerMask.insert(.bottomRight)
            }
            let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: cornerMask, cornerRadii: CGSize(width: radius, height: radius))
            let mask = CAShapeLayer()
            mask.path = path.cgPath
            self.layer.mask = mask
        }
    }
    

    }

    0 讨论(0)
  • 2020-11-27 13:17

    Swift 4+

    func roundCorners(with CACornerMask: CACornerMask, radius: CGFloat) {
              self.layer.cornerRadius = radius
              self.layer.maskedCorners = [CACornerMask]
        }
    

    Top right

    roundCorners(with: [.layerMinXMinYCorner], radius: 20)
    

    Top left

    roundCorners(with: [.layerMaxXMinYCorner], radius: 20)
    

    Bottom right

    roundCorners(with: [.layerMinXMaxYCorner], radius: 20)
    

    Bottom left

    roundCorners(with: [.layerMaxXMaxYCorner], radius: 20)
    

    OR

    Multiple corners at the same time

        func roundedCorners(corners : UIRectCorner, radius : CGFloat) {
            let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
            let mask = CAShapeLayer()
            mask.path = path.cgPath
            layer.mask = mask
        }
    

    How to use

    roundedCorners(corners: [.topLeft, .topRight], radius: 20)
    
    0 讨论(0)
  • 2020-11-27 13:17
    1. Add RoundedCornerView.swift file to your project
    2. Add an UIView to your ViewController
    3. Change Custom Class in Identity Inspector to RoundedCornerView
    4. Go to Attribute Inspector pick Corner Radius (CGFloat) and On the corners you want rounded.

    RoundedCornerView.swift

    import UIKit
    
    @IBDesignable
    class RoundedCornerView: UIView {
    
        var cornerRadiusValue : CGFloat = 0
        var corners : UIRectCorner = []
    
        @IBInspectable public var cornerRadius : CGFloat {
            get {
                return cornerRadiusValue
            }
            set {
                cornerRadiusValue = newValue
            }
        }
    
        @IBInspectable public var topLeft : Bool {
            get {
                return corners.contains(.topLeft)
            }
            set {
                setCorner(newValue: newValue, for: .topLeft)
            }
        }
    
        @IBInspectable public var topRight : Bool {
            get {
                return corners.contains(.topRight)
            }
            set {
                setCorner(newValue: newValue, for: .topRight)
            }
        }
    
        @IBInspectable public var bottomLeft : Bool {
            get {
                return corners.contains(.bottomLeft)
            }
            set {
                setCorner(newValue: newValue, for: .bottomLeft)
            }
        }
    
        @IBInspectable public var bottomRight : Bool {
            get {
                return corners.contains(.bottomRight)
            }
            set {
                setCorner(newValue: newValue, for: .bottomRight)
            }
        }
    
        func setCorner(newValue: Bool, for corner: UIRectCorner) {
            if newValue {
                addRectCorner(corner: corner)
            } else {
                removeRectCorner(corner: corner)
            }
        }
    
        func addRectCorner(corner: UIRectCorner) {
            corners.insert(corner)
            updateCorners()
        }
    
        func removeRectCorner(corner: UIRectCorner) {
            if corners.contains(corner) {
                corners.remove(corner)
                updateCorners()
            }
        }
    
        func updateCorners() {
            let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: cornerRadiusValue, height: cornerRadiusValue))
            let mask = CAShapeLayer()
            mask.path = path.cgPath
            self.layer.mask = mask
        }
    
    }
    

    github link : RoundedCornerView

    0 讨论(0)
  • 2020-11-27 13:20

    You just need to mask the layer as shown below:

    For Swift 3:

    let rectShape = CAShapeLayer()
    rectShape.bounds = self.myView.frame
    rectShape.position = self.myView.center
    rectShape.path = UIBezierPath(roundedRect: self.myView.bounds, byRoundingCorners: [.bottomLeft , .bottomRight , .topLeft], cornerRadii: CGSize(width: 20, height: 20)).cgPath
    
    self.myView.layer.backgroundColor = UIColor.green.cgColor
    //Here I'm masking the textView's layer with rectShape layer
    self.myView.layer.mask = rectShape
    

    Lower Version:

    let rectShape = CAShapeLayer()
    rectShape.bounds = self.myView.frame
    rectShape.position = self.myView.center
    rectShape.path = UIBezierPath(roundedRect: self.myView.bounds, byRoundingCorners: .BottomLeft | .BottomRight | .TopLeft, cornerRadii: CGSize(width: 20, height: 20)).CGPath
    
    self.myView.layer.backgroundColor = UIColor.greenColor().CGColor
    //Here I'm masking the textView's layer with rectShape layer
    self.myView.layer.mask = rectShape
    
    0 讨论(0)
  • 2020-11-27 13:20

    Swift 3

    extension UIView {
        func roundCorners(_ corners:UIRectCorner, radius: CGFloat) {
        let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
        let mask = CAShapeLayer()
        mask.path = path.cgPath
        self.layer.mask = mask
      }
    }
    

    Use like this

    YourView.roundCorners([.topLeft, .bottomLeft], radius: 10)
    
    0 讨论(0)
  • 2020-11-27 13:20

    Swift 4

    override func viewDidLoad() {
        let topRight = UIView(frame: CGRect(x: 120, y: 200, width: 120, height: 120))
        topRight.roundedTop()
        topRight.backgroundColor = .red
        self.view.center = topRight.center
        self.view.addSubview(topRight)
        super.viewDidLoad()
    }
    

    Output :

    extension on UIView Swift 4 : Reference Link

    0 讨论(0)
提交回复
热议问题