sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode:iOS7替代

时间:2013-10-07 11:59:00

标签: ios7 sizewithfont

请帮我找一个弃用方法的替代方法......

CGSize size = [title sizeWithFont:buttonFont
                                  minFontSize:10
                               actualFontSize:nil
                                     forWidth:_view.bounds.size.width
                                lineBreakMode:NSLineBreakByClipping];

可以(boundingRectWithSize:options:attributes:context :)执行此操作吗?像这样......

    NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                [UIFont systemFontOfSize:10], NSFontAttributeName,
                                nil];

    CGSize size = [title boundingRectWithSize:CGSizeMake(_view.bounds.size.width-kBorder*2, _view.bounds.size.height)
                                      options:NSLineBreakByClipping
                                   attributes:attributes
                                      context:nil].size;

我是对的吗? 期待你的建议:)

1 个答案:

答案 0 :(得分:1)

使用以下代码查看我之前的答案here

- (CGSize)text:(NSString *)text sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size
{
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(7.0))
    {
        NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                          font, NSFontAttributeName,
                                          nil];

        CGRect frame = [text boundingRectWithSize:size
                                          options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading)
                                       attributes:attributesDictionary
                                          context:nil];

        return frame.size;
    }
    else
    {
        return [text sizeWithFont:font constrainedToSize:size];
    }
}