Reputation: 1380
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
Upvotes: 52
Views: 34650
Reputation: 4248
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.
Upvotes: 145
Reputation: 6023
Swift 3/4/5
yourlabel.layer.cornerRadius = 8 //your desire radius
yourlabel.layer.masksToBounds = true
Upvotes: 9
Reputation: 2199
Swift 2 solution :
@IBOutlet weak var your_label: UILabel!
your_label.layer.cornerRadius = 5
your_label.layer.masksToBounds = true
Upvotes: 1
Reputation: 217
You can use below code,
[[myLabel layer] setCornerRadius:5.0f];
[[myLabel layer] setMasksToBounds:YES];
thanks,
Upvotes: 0
Reputation: 2764
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
Upvotes: 1