How to round edges of UILabel with Swift
IosSwiftIos Problem Overview
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.
Ios Solutions
Solution 1 - Ios
The trick is to set maskToBounds
to true. Then your modifications to cornerRadius
will be visible.
label.layer.masksToBounds = true
label.layer.cornerRadius = 5
Solution 2 - Ios
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
Solution 3 - Ios
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.
Solution 4 - Ios
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];
Solution 5 - Ios
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.
Solution 6 - Ios
Select your UILabel
In your inspector, add layer.masksToBounds
and layer.cornerRadius
Less code to handle in your classes
Solution 7 - Ios
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
Solution 8 - Ios
> 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.
Solution 9 - Ios
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
}
}
}