uitableview - sizeWithFont :constrainedToSize:lineBreakMode: deprecated in iOS7

标签 uitableview ios7 deprecated

我正在将我的应用程序更新到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);
}

但是在iOS 7中使用它时会出现错误消息:

Use -boundingRectWithSize:options:attributes:context:



我不知道如何将较早的版本转换为这种新方法,如果有人可以帮助我,那将是很好的。提前致谢。

最佳答案

在iOS7中不推荐使用 sizeWithFont 方法。您应该改为使用 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];
}

关于uitableview - sizeWithFont :constrainedToSize:lineBreakMode: deprecated in iOS7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18834275/

相关文章:

ios - 推送和弹出 VC 后,UITableViewCell selectedBackgroundView 保持按下状态

IOS 如何在 Collection View 中只下载合适的图片?

arrays - 在 UITableView 中按时间顺序对 Firebase 帖子进行排序(最新的在前)

php - 使用旧版 codeigniter 站点设置魔术引号 PHP 错误

css - 已弃用的 CSS 属性列表

ios - 我可以为自己在 Objective-C 中的弃用添加修复吗?

ios - 如何更改 UITableView Swift 3 中的分隔符高度?

javascript - 如何区分 iOS7 用户代理字符串中的 Chrome 和 Safari?

objective-c - 如何在iOS7风格上实现 `viewForHeaderInSection`?

ios - 扩展/收缩 UITableViewCell 高度在 iOS7 中不再起作用