Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Corner Radius property of UILabel is not working in iOS 7.1

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

like image 791
Surfer Avatar asked Mar 12 '14 10:03

Surfer


People also ask

How do you add corner radius to a storyboard?

Select the view that you want to round and open its Identity Inspector. In the User Defined Runtime Attributes section, add the following two entries: Key Path: layer. cornerRadius , Type: Number, Value: (whatever radius you want)

How do you label corner radius in Swift?

"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..

How do you round UIView corners?

If you start with a regular UIView it has square corners. You can give it round corners by changing the cornerRadius property of the view's layer . and smaller values give less rounded corners. Both clipsToBounds and masksToBounds are equivalent.


2 Answers

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.

like image 122
iOS Dev Avatar answered Oct 04 '22 12:10

iOS Dev


Swift 3/4/5

    yourlabel.layer.cornerRadius = 8 //your desire radius     yourlabel.layer.masksToBounds = true 
like image 41
Shakeel Ahmed Avatar answered Oct 04 '22 12:10

Shakeel Ahmed