Question

Since the update to iOS7.1 the UILabels in my app have changed appearance. Prior to the OS update, all my UILabels had rounded corners as was my intention. With iOS7.1 they now all have sharp corners.

I insert the code I use below. Any help on how to restore the UILabels to my original appearance will be appreciated.



Top of the implementation file of the view controller:

#import <QuartzCore/QuartzCore.h>

The code for the creation of the UILabel. I have marked (what I consider) the relevant line with a comment.

    CGRect labelRect = CGRectMake(20, 20, 200, 100);

    NSString *theText = [self info];
    CGSize size = [theText sizeWithFont:[UIFont systemFontOfSize:15] constrainedToSize:CGSizeMake(labelRect.size.width, 10000) lineBreakMode:NSLineBreakByWordWrapping];

    UILabel *theLabel = [[UILabel alloc] initWithFrame:labelRect];
    [theLabel setNumberOfLines:0];
    [theLabel setText:theText];
    [[theLabel layer] setCornerRadius:15]; // THIS IS THE RELEVANT LINE

    // For iOS7
    if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1)
        [theLabel setBackgroundColor:[UIColor whiteColor]];

    [[self view] addSubview:theLabel];
Was it helpful?

Solution

Set theLabel.clipsToBounds = YES, or theLabel.layer.masksToBounds = YES. Either will work.

OTHER TIPS

use this

CGRect labelRect = CGRectMake(20, 20, 200, 100);

NSString *theText = [self info];
CGSize size = [theText sizeWithFont:[UIFont systemFontOfSize:15] constrainedToSize:CGSizeMake(labelRect.size.width, 10000) lineBreakMode:NSLineBreakByWordWrapping];

UILabel *theLabel = [[UILabel alloc] initWithFrame:labelRect];
[theLabel setNumberOfLines:0];
[theLabel setText:theText];
[[theLabel layer] setCornerRadius:15]; // THIS IS THE RELEVANT LINE
[theLabel.layer setMasksToBounds:YES]; ///missing in your code
// For iOS7
if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1)
    [theLabel setBackgroundColor:[UIColor whiteColor]];

[[self view] addSubview:theLabel];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top