在ios 7中替换弃用sizeWithFont:minFontSIze:actualFontSize [英] Replace the deprecation sizeWithFont:minFontSIze:actualFontSize in ios 7
问题描述
在iOS 6中我使用的是这种方法:
In iOS 6 i was using this method:
[self.handText sizeWithFont:font
minFontSize:10.0f
actualFontSize:&maxFontSize
forWidth:handWidth/2
lineBreakMode:UILineBreakModeClip];
xcode 5表示'sizeWithFont:minFontSIze:actualFontSize:forWidth:lineBreakMode: '已被弃用:首先在iOS 7中弃用
xcode 5 says that 'sizeWithFont:minFontSIze:actualFontSize:forWidth:lineBreakMode:' is deprecated:first deprecated in iOS 7
现在我实现如下:
[self.handText sizeWithAttributes:@{NSFontAttributeName:font}
minFontSize:10.0f
actualFontSize:&maxFontSize
forWidth:handWidth/2
lineBreakMode:NSLineBreakByClipping];
此处xcode抛出另一个警告说:
'实例方法 - sizeWithAttributed:minFontSize:forWidth:lineBreakMode:'not found(返回类型默认为'id')
here xcode throws another warning saying:
'Instance method -sizeWithAttributed:minFontSize:forWidth:lineBreakMode:'not found(return type defaults to 'id')
任何人都可以帮我解决这个问题警告。
Can anyone please help me to fix this warning.
推荐答案
请改用此辅助方法:
-(CGSize)frameForText:(NSString*)text sizeWithFont:(UIFont*)font constrainedToSize:(CGSize)size lineBreakMode:(NSLineBreakMode)lineBreakMode {
NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];
paragraphStyle.lineBreakMode = lineBreakMode;
NSDictionary * attributes = @{NSFontAttributeName:font,
NSParagraphStyleAttributeName:paragraphStyle
};
CGRect textRect = [text boundingRectWithSize:size
options:NSStringDrawingUsesLineFragmentOrigin
attributes:attributes
context:nil];
//Contains both width & height ... Needed: The height
return textRect.size;
}
如果您需要同时支持iOS 6和iOS 7,请使用如下所示:
Use like so, if you need to support both iOS 6 and iOS 7:
#ifdef __IPHONE_7_0
titleSize = [self frameForText:self.titleLabel.text sizeWithFont:self.titleLabel.font constrainedToSize:CGSizeMake(labelMaxWidth,self.titleLabel.font.lineHeight) lineBreakMode:self.titleLabel.lineBreakMode ];
subtitleSize = [self frameForText:self.subtitleLabel.text sizeWithFont:self.subtitleLabel.font constrainedToSize:CGSizeMake(labelMaxWidth,self.subtitleLabel.font.lineHeight) lineBreakMode:self.subtitleLabel.lineBreakMode];
#else
titleSize = [self.titleLabel.text sizeWithFont:self.titleLabel.font
constrainedToSize:CGSizeMake(labelMaxWidth,self.titleLabel.font.lineHeight)
lineBreakMode:self.titleLabel.lineBreakMode];
subtitleSize = [self.subtitleLabel.text sizeWithFont:self.subtitleLabel.font
constrainedToSize:CGSizeMake(labelMaxWidth,self.subtitleLabel.font.lineHeight)
lineBreakMode:self.subtitleLabel.lineBreakMode];
#endif
这篇关于在ios 7中替换弃用sizeWithFont:minFontSIze:actualFontSize的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!