替换ios 7中的弃用sizeWithFont:minFontSIze:actualFontSize

时间:2013-12-26 13:54:50

标签: iphone ios7 uifont

在iOS 6中我使用的是这种方法:

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

xcode 5表示'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抛出另一个警告说: 'Instance method -sizeWithAttributed:minFontSize:forWidth:lineBreakMode:'not found(return type defaults to 'id')

任何人都可以帮我解决这个警告。

3 个答案:

答案 0 :(得分:8)

请改用此辅助方法:

-(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,请使用如下:

#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

答案 1 :(得分:2)

方法签名是:

- (CGSize)sizeWithAttributes:(NSDictionary *)attrs

这意味着您不能指定比第一个参数更多的参数(属性数组)。因此,您基本上使用的是iOS SDK中不存在的方法(sizeWithAttributed:minFontSize:forWidth:lineBreakMode:)。

要获得解决方法,请查看this question

答案 2 :(得分:1)

你可以使用这种方法

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