I have looked in the attributes of labels in Xcode 6.3 but I have not found out how to round the edges in the same way that you can round the edges of a text field.
-
2possible duplicate of [How do I create a round cornered UILabel on the iPhone?](http://stackoverflow.com/questions/510382/how-do-i-create-a-round-cornered-uilabel-on-the-iphone) – sylvanaar Jun 30 '15 at 19:09
9 Answers
The trick is to set maskToBounds
to true. Then your modifications to cornerRadius
will be visible.
label.layer.masksToBounds = true
label.layer.cornerRadius = 5

- 19,173
- 10
- 77
- 88
-
1**"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.. **And Thank You @spencer.sm!!** – Prasad Patil Jun 05 '18 at 11:40
-
Assuming you have added a backgroundColor
to your label otherwise there would be no way to tell if it had edges, you can use QuartzCore to round the edges of a label.
import QuartzCore
yourLabel.layer.backgroundColor = UIColor.redColor().CGColor
yourLabel.layer.cornerRadius = 5
yourLabel.layer.masksToBounds = true

- 1,598
- 19
- 25

- 8,932
- 4
- 43
- 64
Use label layer corner radius to do that,
mylabel.layer.cornerRadius = yourvalue
if you don't want to show shadow then add,
mylabel.layer.masksToBounds = true
it worked for me fine.

- 398
- 5
- 14

- 868
- 9
- 20
-
3
-
The `clipToBounds` property can also be enabled in Interface Builder (Storyboard) in the Attributes inspector of the Label, under the Drawing section of the View. – Jonathan Cabrera Jul 24 '18 at 17:38
-
@JamesHeald NO, clipToBounds is for UIView, and masksToBounds is for CALayer. – Y.Bi Mar 13 '19 at 05:09
You can give cornerRadius
and border like this.
For Swift 5, 4 and 3
MyLable.layer.masksToBounds = true
MyLable.layer.cornerRadius = 6
MyLable.layer.borderWidth = 2
MyLable.layer.borderColor = UIColor.black.cgColor
For Objective-C
[MyLable.layer setMasksToBounds:TRUE];
[MyLable.layer setCornerRadius:6];
[MyLable.layer setBorderWidth:2];
[MyLable.layer setBorderColor:[UIColor blackColor].CGColor];

- 673
- 5
- 10
Select your UILabel
In your inspector, add layer.masksToBounds
and layer.cornerRadius
Less code to handle in your classes

- 2,037
- 2
- 23
- 32

- 346
- 2
- 6
-
This is a good solution to assign the property in design time without code – Lorenzo Sep 30 '20 at 07:34
A way to round the corners of any CALayer
is to modify layer.cornerRadius
. By default that will affect only the background colour and any layer border you've applied. You can also enable clipsToBounds
to clip the pixel contents.
When a view, such as a UILabel
draws itself, it draws itself to the pixel contents of a layer. You can grab view.layer
to access the layer.
So you can set that layer's corner radius to affect the background colour of the view. You can also enable clipsToBounds
to crop any content within the view. Provided the view itself isn't drawing too close to the edge, that should achieve what you want. It should be correct for labels.

- 99,986
- 12
- 185
- 204
-
4Great answer! Basically it means you have to add this line: L.backgroundColor = UIColor.blackColor() ; L.layer.cornerRadius = 5.0 ; L.clipsToBounds = true // where L is your UILabel – Nitin Nain Feb 01 '16 at 16:55
-
Swift:
//round shape corner radius set
self.lblName.layer.masksToBounds = true
self.lblName.layer.cornerRadius = self.lblName.bounds.width / 2
//custom shape corner radius set
self.lblName.layer.masksToBounds = true
self.lblName.layer.cornerRadius = 12

- 2,187
- 15
- 28
Works fine in Xcode 8.1.2 with Swift 3, Tested during AUGUST 2017
"cornerRadius" is the key property to set the rounded edges, where if you are using the same style for all the labels in your application, I would recommend for an extension method.
Code:
// extension Class
extension UILabel {
// extension user defined Method
func setRoundEdge() {
let myGreenColor = (UIColor(red: -0.108958, green: 0.714926, blue: 0.758113, alpha: 1.0))
//Width of border
self.layer.borderWidth = 1.0
//How much the edge to be rounded
self.layer.cornerRadius = 5.0
// following properties are optional
//color for border
self.layer.borderColor = myGreenColor.cgColor
//color for text
self.textColor = UIColor.red
// Mask the bound
self.layer.masksToBounds = true
//clip the pixel contents
self.clipsToBounds = true
}
}
Output:
Why Extension method?
Create a Swift file and add the following code, which has the Extention method to the "UILabel" class, where this method is user defined but will work for all the label in your application and will help to maintain consistency and clean code, if you change any style in future require only in the extension method.

- 2,747
- 1
- 33
- 33
You create an extension for UIView with IBInspectable
extension UIView {
@IBInspectable var cornerRadius: CGFloat {
get {
return layer.cornerRadius
}
set {
layer.cornerRadius = newValue
layer.masksToBounds = newValue > 0
}
}
@IBInspectable var borderWidth: CGFloat {
get {
return layer.borderWidth
}
set {
layer.borderWidth = newValue
}
}
@IBInspectable var borderColor: UIColor? {
get {
return UIColor(cgColor: layer.borderColor!)
}
set {
layer.borderColor = newValue?.cgColor
}
}
}

- 810
- 12
- 12