Replace the deprecation sizeWithFont:minFontSIze:actualFontSize in ios 7

泄露秘密 提交于 2019-12-03 13:25:14

问题


In iOS 6 i was using this method:

[self.handText sizeWithFont:font 
 minFontSize:10.0f 
 actualFontSize:&maxFontSize 
 forWidth:handWidth/2 
 lineBreakMode:UILineBreakModeClip];

xcode 5 says that 'sizeWithFont:minFontSIze:actualFontSize:forWidth:lineBreakMode:' is deprecated:first deprecated in iOS 7

Now i implemented like this:

[self.handText sizeWithAttributes:@{NSFontAttributeName:font} 
 minFontSize:10.0f 
 actualFontSize:&maxFontSize 
 forWidth:handWidth/2 
 lineBreakMode:NSLineBreakByClipping];

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.


回答1:


Use this helper method instead:

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

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



回答2:


The method signature is:

- (CGSize)sizeWithAttributes:(NSDictionary *)attrs

which means you cannot specify any more argument than the first one (array of attributes). So, you are basically using a method (sizeWithAttributed:minFontSize:forWidth:lineBreakMode:) which is not existing in the iOS SDK.

For a workaround, please have a look at this question.




回答3:


you can use this method

- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(NSDictionary *)attributes context:(NSStringDrawingContext *)context NS_AVAILABLE_IOS(7_0);


来源:https://stackoverflow.com/questions/20786067/replace-the-deprecation-sizewithfontminfontsizeactualfontsize-in-ios-7

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!