SizeWithFont在IOS7中已弃用

时间:2014-11-03 05:46:45

标签: objective-c ios7

如何更改

CGSize size=[tempStr sizeWithFont:[UIFont boldSystemFontOfSize:17]  constrainedToSize:CGSizeMake(200, 56)];

使其适用于IOS7

2 个答案:

答案 0 :(得分:2)

您必须使用sizeWithAttributes:,例如:

UIFont *font = [UIFont boldSystemFontOfSize:17.0f];
NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
CGSize size = [tempStrig sizeWithAttributes:attrDictionary];

答案 1 :(得分:1)

    CGFloat finalHeight;

    CGSize constrainedSize = CGSizeMake(requiredWidth,9999);

    NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:yourRequiredFont, NSFontAttributeName,nil];

    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:yourText attributes:attributesDictionary];

    CGRect requiredHeight = [string boundingRectWithSize:constrainedSize options:NSStringDrawingUsesLineFragmentOrigin context:nil];

    if (requiredHeight.size.width > requiredWidth) {
        requiredHeight = CGRectMake(0,0,requiredWidth, requiredHeight.size.height);
    }

    finalHeight=requiredHeight.size.height;