Objective c 为sizeWithFont替换不推荐使用的IOS 7函数。如何使它更优雅?

Objective c 为sizeWithFont替换不推荐使用的IOS 7函数。如何使它更优雅?,objective-c,ios7,Objective C,Ios7,请注意,该代码有3个问题: 对于-(CGSize)sizeWithFont2:(UIFont*)字体约束大小:(CGSize)size lineBreakMode:(NSLineBreakMode)lineBreakMode,完全不使用参数lineBreakMode。我不知道如何在IOS 7中使用它。我在stackOverflow中四处查看,那里的答案也没有使用该参数 此外,我认为IOS 6中的sizeWithFont:必须调用sizeWithFont:ConstrainedToSize:但使用

请注意,该代码有3个问题:

  • 对于
    -(CGSize)sizeWithFont2:(UIFont*)字体约束大小:(CGSize)size lineBreakMode:(NSLineBreakMode)lineBreakMode
    ,完全不使用参数lineBreakMode。我不知道如何在IOS 7中使用它。我在stackOverflow中四处查看,那里的答案也没有使用该参数
  • 此外,我认为IOS 6中的sizeWithFont:必须调用sizeWithFont:ConstrainedToSize:但使用默认大小。但是尺寸是多少呢
  • 最后我在
    [self-sizeWithFont:font]中得到警告因为它是一个不推荐使用的函数。我想删除那个警告
  • 建议?

    第3点:

    如果使用SDK7,则始终会出现此警告或错误

    第2点:

    你可以选择你喜欢的尺码

    -(CGSize) sizeWithFont2:(UIFont *)font
    {
        if ([self respondsToSelector:@selector(boundingRectWithSize:options:attributes:context:)])
        {
            CGSize result = [self sizeWithAttributes:@{NSFontAttributeName:font}];
            return result;
        }
        return [self sizeWithFont:font];
    }
    - (CGSize) sizeWithFont2:(UIFont *)font constrainedToSize:(CGSize)size
    {
        if ([self respondsToSelector:@selector(boundingRectWithSize:options:attributes:context:)])
        {
            CGRect frame = [self boundingRectWithSize:size
                                              options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading)
                                           attributes:@{NSFontAttributeName:font}
                                              context:nil];
            return frame.size;
        }
        else
        {
            return [self sizeWithFont:font constrainedToSize:size];
        }
    }
    
    - (CGSize) sizeWithFont2:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(NSLineBreakMode) lineBreakMode
    {
        return [self sizeWithFont2:font constrainedToSize:size]; //the NSLineBreakMode not used?
    
    }
    
    你可以选择300作为宽度,因为它是屏幕大小,有一点空白。
    但是,您应该使用自己的字体和大小值。

    为什么不使用CGSizeMake(FLT\u MAX,FLT\u MAX);最好的方法是使用NSAttributedString获取字符串的宽度。我这样做是为了计算文本视图的高度:
    CGSize maximumSize = CGSizeMake(300, FLT_MAX);