Rounded corners of UILabel swift

Astha Gupta picture Astha Gupta · Sep 7, 2017 · Viewed 17.9k times · Source

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?

Answer

Son Pham picture Son Pham · Sep 7, 2017

I think you should set maskToBounds for textLabel. try this:

textLabel?.layer.masksToBounds = true