2013-10-09 40 views
-1

由于新的iOS7 sizeWithFont:constrainedToSize:lineBreakMode已被弃用,我在我的XCode 5中收到有关它的警告。我不得不说,根据我的说法,它不会影响功能,但我希望找到替代方案为了消除恼人的警告。这里是我的相关问题代码:sizeWithFont在iOS7中已被弃用

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

和:

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

我不能由我自己来弄明白一个解决方案,我不知道改用什么。

+1

看看这里:http://stackoverflow.com/questions/19028743/ios7-uitextview-contentsize-height-alternative/19067476#19067476 –

+1

带有sizeWithFont的谷歌搜索已弃用iOS 7为您的问题提供了一个完美有用的修复方法。总是首先Google。 – dandan78

+0

+1,我想知道同样的事情。事情是我想知道从旧代码到新代码的确切转换。该文件没有给出。 –

回答

1
boundingRectWithSize:options:attributes:context: instead. 

只是检查苹果文档:

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

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 
    } 
} 

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