简体   繁体   中英

Creating a dynamic label with boundingRectWithSize:options:attributes:context:

I'm trying to set my label size to be dynamic, however this current method is deprecated. I know that the correct method is:

boundingRectWithSize:options:attributes:context:

This really doesn't do the trick for modifying the font size along with line break mode.

CGSize expectedLabelSize;
expectedLabelSize = [textLabel.text sizeWithFont:[UIFont fontWithName:@"Ubuntu-Bold" size:14] constrainedToSize:maximumLabelSize
                                       lineBreakMode:NSLineBreakByWordWrapping];

Is there another method that should be used to change the attributes for my label?

Just try this..

NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName,nil];
CGSize labelContraints              = CGSizeMake(width, 105.0);//Here I set maximum height as 105 for maximum of 5 lines.
NSStringDrawingContext *context     = [[NSStringDrawingContext alloc] init];
CGRect labelRect                    = [str boundingRectWithSize:labelContraints
    options:NSStringDrawingUsesLineFragmentOrigin attributes:attributesDictionary 
context:context];

Note: It will working IOS7+

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM