Home » Swift » How to round edges of UILabel with Swift

How to round edges of UILabel with Swift

Posted by: admin November 30, 2017 Leave a comment

Questions:

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.

Answers:

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

Questions:
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

Questions:
Answers:

Use label layer corner raious to do that,

mylabel.layer.cornerRadius = yourvalue

if you dont want to shadow then add

mylabel.layer.masksToBounds = true 

it Worked for me fine.

Questions:
Answers:

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.

Questions:
Answers:

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:

enter image description here

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.