I am working in Xcode 7 beta 3. I want to create a label with a rounded rectangle background. By default, I can create a background with my color of choice but I can't round the corners. I have tried creating an outlet for that label and then in viewDidLoad()
I wrote this code label.layer.cornerRadius = 10
. I didn't get any errors but it didn't change the label in the simulator. Does anyone know how to do this in Swift 2?
I assume you're setting layer. cornerRadius in the user defined runtime attributes and are expecting the background color rectangle to have rounded corners? Check if you've enabled "Clip to Bounds" in the view drawing options, that's off by default for labels but is required for the rounded corners to work.
If you want only some corners to be rounded, you should set the layer's maskedCorners property to be an array of the corners you want – it's an option set, so you can set one or many depending on your needs.
The most simplest approach is to add this attribute to the label you want to have rounded corners.
layer.cornerRadius
And also enable Clip Subviews property as well.
You'll also want to clip to bounds of the label:
myLabel.backgroundColor = UIColor.blueColor()
myLabel.layer.cornerRadius = 10.0
myLabel.clipsToBounds = true
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