sizeWithFont:constrainedToSize:lineBreakMode:iOS7中不推荐使用

use*_*439 6 deprecated uitableview ios7

我正在将我的应用程序更新到iOS 7并最终得到它,但有一件事我无法找到解决方案.

在Xcode 4中,我使用了以下方法:

#define FONT_SIZE 14.0f
#define CELL_CONTENT_WIDTH 280.0f
#define CELL_CONTENT_MARGIN 10.0f


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath; {
    NSString *text = [textA objectAtIndex:[indexPath row]];

    CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);

    CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:NSLineBreakByWordWrapping];

    CGFloat height = MAX(size.height, 28.0f);

    return height + (CELL_CONTENT_MARGIN * 2);
}
Run Code Online (Sandbox Code Playgroud)

但是在iOS 7中使用它时会出错:

使用-boundingRectWithSize:options:attributes:context:

我不知道如何将我的早期版本转换为这种新方法,如果有人能帮助我,那将会很棒.提前致谢.

Igo*_*gin 5

sizeWithFont方法在iOS7中已弃用.您应该使用boundingRectWithSize.如果您还需要支持以前的iOS版本,那么您可以使用以下代码:

CGSize size = CGSizeZero;

if ([label.text respondsToSelector: @selector(boundingRectWithSize:options:attributes:context:)] == YES) {
    size = [label.text boundingRectWithSize: constrainedSize options: NSStringDrawingUsesLineFragmentOrigin
                                 attributes: @{ NSFontAttributeName: label.font } context: nil].size;
} else {
    size = [label.text sizeWithFont: label.font constrainedToSize: constrainedSize lineBreakMode: UILineBreakModeWordWrap];
}
Run Code Online (Sandbox Code Playgroud)