I am creating a UILabel
programatically. But the below piece of code doesn't give me rounded corners. I think I am missing out something very basic.
var textLabel:UILabel? = UILabel()
textLabel?.text = text
textLabel?.frame = CGRect(x:point.x, y:point.y, width:(textLabel?.intrinsicContentSize.width)!, height:15)
textLabel?.backgroundColor = UIColor.white
textLabel?.font = UIFont(name:"OpenSans", size:8)
textLabel?.sizeToFit()
textLabel?.layer.cornerRadius = 20.0
Can anyone please point me in the right direction?
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..
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.
The corner radius is a measurement describing the curve on the corners of your labels. This is measured in millimetres and refers to the radius of the circle created if the curve was extended to create a full circle.
I think you should set maskToBounds for textLabel. try this:
textLabel?.layer.masksToBounds = true
try this :-
textLabel?.layer.cornerRadius = textLabel?.frame.size.height/2.0
textLabel?.layer.masksToBounds = true
if you want to set border color then :-
textLabel?.layer.borderColor = .red.cgColor
textLabel?.layer.borderWidth = 1.0
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