Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set layer cornerRadius for only bottom-left, bottom-right, and top-left corner?

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.

like image 748
Zept Avatar asked Jul 05 '15 16:07

Zept


People also ask

How do you change the corner radius of a storyboard?

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)

How do you label corner radius in Swift?

"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..


2 Answers

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 
like image 33
Shailesh Chandavar Avatar answered Sep 17 '22 02:09

Shailesh Chandavar


(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 :)

like image 85
Fabio Avatar answered Sep 21 '22 02:09

Fabio