51

I am setting the cornerRadius property for UILabel. Its working fine all version of iOS < 7.1. Following code i have used,

UILabel *originaltagLbl = [[UILabel alloc]initWithFrame:CGRectMake(startX, startY, 100,30)];    
[originaltagLbl setFont:[UIFont fontWithName:@"MuseoSans-500" size:15]];
[originaltagLbl setTextAlignment:NSTextAlignmentCenter];
[originaltagLbl setTextColor:UIColorFromRGB(0xffffff)];
originaltagLbl.backgroundColor = [UIColor redColor];
originaltagLbl.layer.cornerRadius = 5;
originaltagLbl.layer.borderColor = [UIColor redColor].CGColor;
originaltagLbl.layer.borderWidth = 1;
[scrollView addSubview:originaltagLbl];

if i use this, just simply displaying the label as rectanglular box, So how to set the corner radius of UILabel in iOS 7.1

Surfer
  • 1,370
  • 3
  • 19
  • 34

6 Answers6

143

Add the next line to your code:

// Swift:
originaltagLbl.layer.masksToBounds = true

// Objective C:
originaltagLbl.layer.masksToBounds = YES;

For information see this SO answer, or read documentation.

iOS Dev
  • 4,143
  • 5
  • 30
  • 58
9

Swift 3/4/5

    yourlabel.layer.cornerRadius = 8 //your desire radius
    yourlabel.layer.masksToBounds = true
Shakeel Ahmed
  • 5,361
  • 1
  • 43
  • 34
2

Try setting the UILabel's clipsToBounds property to YES

nnarayann
  • 1,449
  • 19
  • 24
1

It's true that clipsToBounds works in 7.1, but the problem is in situations where you're scrolling/animating it's really slow and makes everything laggy.

Setting the background color on the layer instead of the uiview is all that is needed.

See: UILabel layer cornerRadius negatively impacting performance

Community
  • 1
  • 1
Aaron Zinman
  • 2,696
  • 2
  • 19
  • 16
1

Swift 2 solution :

@IBOutlet weak var your_label: UILabel!

your_label.layer.cornerRadius = 5
your_label.layer.masksToBounds = true
Fox5150
  • 2,180
  • 22
  • 24
0

You can use below code,

[[myLabel layer] setCornerRadius:5.0f];
[[myLabel layer] setMasksToBounds:YES];

thanks,

Kupendiran iOS
  • 217
  • 2
  • 5