Question

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

Was it helpful?

Solution

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.

OTHER TIPS

Swift 3/4/5

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

Try setting the UILabel's clipsToBounds property to YES

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

Swift 2 solution :

@IBOutlet weak var your_label: UILabel!

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

You can use below code,

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

thanks,

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top