Surfer
Surfer

Reputation: 1380

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

Upvotes: 52

Views: 34650

Answers (6)

iOS Dev
iOS Dev

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

Shakeel Ahmed
Shakeel Ahmed

Reputation: 6023

Swift 3/4/5

    yourlabel.layer.cornerRadius = 8 //your desire radius
    yourlabel.layer.masksToBounds = true

Upvotes: 9

Fox5150
Fox5150

Reputation: 2199

Swift 2 solution :

@IBOutlet weak var your_label: UILabel!

your_label.layer.cornerRadius = 5
your_label.layer.masksToBounds = true

Upvotes: 1

Kupendiran iOS
Kupendiran iOS

Reputation: 217

You can use below code,

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

thanks,

Upvotes: 0

Aaron Zinman
Aaron Zinman

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

nnarayann
nnarayann

Reputation: 1449

Try setting the UILabel's clipsToBounds property to YES

Upvotes: 2

Related Questions