2013-10-09 76 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 
    } 
} 

注意:如果它們完全相同,爲什麼蘋果必須貶低舊的?