Replace the deprecation sizeWithFont:minFontSIze:actualFontSize in ios 7

寵の児 提交于 2019-12-03 03:27:59

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
sergio

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.

Kingiol

you can use this method

- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(NSDictionary *)attributes context:(NSStringDrawingContext *)context NS_AVAILABLE_IOS(7_0);
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!