68

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.

Tim
  • 8,206
  • 3
  • 40
  • 64
Michael Paniccia
  • 751
  • 1
  • 5
  • 7
  • 2
    possible duplicate of [How do I create a round cornered UILabel on the iPhone?](http://stackoverflow.com/questions/510382/how-do-i-create-a-round-cornered-uilabel-on-the-iphone) – sylvanaar Jun 30 '15 at 19:09

9 Answers9

188

The trick is to set maskToBounds to true. Then your modifications to cornerRadius will be visible.

label.layer.masksToBounds = true
label.layer.cornerRadius = 5
spencer.sm
  • 14,681
  • 8
  • 67
  • 76
  • **"label.layer.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.. **And Thank You @spencer.sm!!** – Prasad Patil Jun 05 '18 at 11:40
  • Correct Answer! – Android_IT Nov 06 '18 at 10:07
74

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
Jochen Holzer
  • 1,029
  • 11
  • 16
Tim
  • 8,206
  • 3
  • 40
  • 64
34

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.

ttorbik
  • 348
  • 5
  • 14
16

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];
Bera Bhavin
  • 405
  • 3
  • 9
14

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.

Tommy
  • 97,164
  • 12
  • 174
  • 193
  • 4
    Great answer! Basically it means you have to add this line: L.backgroundColor = UIColor.blackColor() ; L.layer.cornerRadius = 5.0 ; L.clipsToBounds = true // where L is your UILabel – Nitin Nain Feb 01 '16 at 16:55
  • Well not really - you absolutely would just subclass it ! – Fattie Jan 19 '20 at 18:32
13

Select your UILabel

In your inspector, add layer.masksToBounds and layer.cornerRadius

Easy way to achieve rounded corners for label

Less code to handle in your classes

Jayesh Thanki
  • 1,879
  • 2
  • 21
  • 29
Erik Nguyen
  • 318
  • 2
  • 6
3

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
Deepak Tagadiya
  • 1,821
  • 11
  • 27
2

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.

BHUVANESH MOHANKUMAR
  • 2,517
  • 1
  • 30
  • 33
1

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

enter image description here

levin varghese
  • 660
  • 11
  • 12