How to create a rounded rectangle label in Xcode 7 and Swift 2

13,187

Solution 1

The most simplest approach is to add this attribute to the label you want to have rounded corners.

layer.cornerRadius

enter image description here

And also enable Clip Subviews property as well.

enter image description here

Solution 2

You'll also want to clip to bounds of the label:

myLabel.backgroundColor = UIColor.blueColor()
myLabel.layer.cornerRadius = 10.0
myLabel.clipsToBounds = true

Solution 3

A view can optionally limit the drawing of its subviews so that any parts of them outside the view are not shown. This is called clipping and is set with the view’s clipsToBounds property.

So you need to add:

label.clipsToBounds = true 

result (Swift 2.0 Xcode 7 iOS9):

Radius of UILabel

Share:
13,187
Michael Montella
Author by

Michael Montella

Updated on July 27, 2022

Comments

  • Michael Montella
    Michael Montella almost 2 years

    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?