Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to round edges of UILabel with Swift

Tags:

ios

swift

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.

like image 836
Michael Paniccia Avatar asked Jun 30 '15 18:06

Michael Paniccia


People also ask

How do you round the corners of a label in Swift?

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

How do you round the corners of a view SwiftUI?

Any SwiftUI view can have its corners rounded using the cornerRadius() modifier. This takes a simple value in points that controls how pronounced the rounding should be.

How do I add padding to UILabel Swift?

If you have created an UILabel programmatically, replace the UILabel class with the PaddingLabel and add the padding: // Init Label let label = PaddingLabel() label. backgroundColor = . black label.


2 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 
like image 86
spencer.sm Avatar answered Sep 25 '22 17:09

spencer.sm


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 
like image 25
Tim Avatar answered Sep 22 '22 17:09

Tim