使用boundingRectWithSize创建动态标签:options:attributes:context:

时间:2014-01-31 10:38:45

标签: ios objective-c

我正在尝试将标签大小设置为动态,但不推荐使用此当前方法。我知道正确的方法是:

boundingRectWithSize:options:attributes:context:

这实际上并不能解决字体大小和换行模式的问题。

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

是否有其他方法可用于更改标签的属性?

1 个答案:

答案 0 :(得分:1)

试试吧..

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];

注意:它将有效IOS7+