How to set corner radius only only bottom-left, bottom-right, and top-left corners of a textview?
let rectShape = CAShapeLayer() rectShape.backgroundColor = UIColor.redColor().CGColor rectShape.bounds = messages.frame rectShape.position = messages.center rectShape.path = UIBezierPath(roundedRect: messages.bounds, byRoundingCorners: .BottomLeft | .TopRight, cornerRadii: CGSize(width: 20, height: 20)).CGPath messages.layer.addSublayer(rectShape)
this code creates two rects. I dont know why.
Select the view that you want to round and open its Identity Inspector. In the User Defined Runtime Attributes section, add the following two entries: Key Path: layer. cornerRadius , Type: Number, Value: (whatever radius you want)
"label. layer. masksToBounds = true" is an important code, if you apply corner radius to a label and if it dosen't work than adding this will definitely add the corner radius to a particular label.. So this should be the correct answer..
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
(swift 4/iOS 11) Just simply say for bottom:
yourView.clipsToBounds = true yourView.layer.cornerRadius = 10 yourView.layer.maskedCorners = [.layerMaxXMaxYCorner, .layerMinXMaxYCorner]
for Up:
yourView.clipsToBounds = true yourView.layer.cornerRadius = 10 yourView.layer.maskedCorners = [.layerMaxXMinYCorner, .layerMinXMinYCorner]
in your case:
yourView.layer.maskedCorners = [.layerMaxXMaxYCorner, .layerMinXMaxYCorner, .layerMinXMinYCorner]
Hope this help :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With