-1

With the new iOS7 sizeWithFont:constrainedToSize:lineBreakMode is deprecated and I receive warnings about it in my XCode 5. I have to say that is not affecting the functionality as far as I can tell but I would like to find an alternative to it in order to remove the annoying warnings. Here's my code related to the problem:

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

and:

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

I wasn't able to figure it out by myself an solution and I don't know what to use instead.

4

3 回答 3

3

如果您阅读文档sizeWithFont:forWidth:lineBreakMode:或头文件,您会阅读应该使用的文件boundingRectWithSize:options:attributes:context:

于 2013-10-09T15:00:06.247 回答
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

于 2013-10-09T14:59:25.837 回答
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
    }
}

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

于 2013-11-04T09:44:10.887 回答