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
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)
"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..
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.
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.
Swift 3/4/5
yourlabel.layer.cornerRadius = 8 //your desire radius yourlabel.layer.masksToBounds = true
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With