sizeWithFont在iOS7中已弃用

时间:2013-10-09 14:54:56

标签: ios objective-c sizewithfont

新的iOS7 sizeWithFont:constrainedToSize:lineBreakMode已被弃用,我在XCode 5中收到有关它的警告。我不得不说这不会影响功能,据我所知,但我想找到一个替代方案它是为了消除恼人的警告。这是我的问题代码:

CGSize minimumLabelSize = [self.subLabel.text sizeWithFont:self.subLabel.font constrainedToSize:maxSize lineBreakMode:NSLineBreakByClipping];

expectedLabelSize = [self.subLabel.text sizeWithFont:self.font constrainedToSize:maximumLabelSize lineBreakMode:NSLineBreakByClipping];

我自己无法解决这个问题,而且我不知道该使用什么。

3 个答案:

答案 0 :(得分:3)

如果您阅读sizeWithFont:forWidth:lineBreakMode:的文档或您已阅读的标题文件,则应使用boundingRectWithSize:options:attributes:context:

答案 1 :(得分:1)

boundingRectWithSize:options:attributes:context: instead.

只需查看Apple文档:

sizeWithFont:constrainedToSize:lineBreakMode:

Returns the size of the string if it were rendered with the specified constraints. (Deprecated in iOS 7.0. Use boundingRectWithSize:options:attributes:context: instead.)

https://developer.apple.com/library/ios/documentation/uikit/reference/NSString_UIKit_Additions/DeprecationAppendix/AppendixADeprecatedAPI.html#//apple_ref/occ/instm/NSString/sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode

答案 2 :(得分:1)

-(CGSize) sizeWithFont2:(UIFont *)font
{
    if ([self respondsToSelector:@selector(boundingRectWithSize:options:attributes:context:)])
    {
        CGSize result = [self sizeWithAttributes:@{NSFontAttributeName:font}];
        return result;
    }
    return [self sizeWithFont:font]; //how to get rid warning here
}
- (CGSize) sizeWithFont2:(UIFont *)font constrainedToSize:(CGSize)size
{
    if ([self respondsToSelector:@selector(boundingRectWithSize:options:attributes:context:)])
    {
        CGRect frame = [self boundingRectWithSize:size
                                          options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading)
                                       attributes:@{NSFontAttributeName:font}
                                          context:nil];
        return frame.size;
    }
    else
    {
        return [self sizeWithFont:font constrainedToSize:size];  //how to get rid warning here
    }
}

注意:如果它们完全相同,为什么苹果必须贬值旧的呢?