Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
519 views
in Technique[技术] by (71.8m points)

uitableview - sizeWithFont:constrainedToSize:lineBreakMode: deprecated in iOS7

I'm updating my app to iOS 7 and finally got it, but there's one thing I can't find a solution for.

In Xcode 4 I used the following method:

#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);
}

But I'll get an error when using it in iOS 7:

Use -boundingRectWithSize:options:attributes:context:

I don't know how to convert my earlier version to this new method and it would be great if anyone could help me. Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

sizeWithFont methods were deprecated in iOS7. You should use boundingRectWithSize instead. If you also need to support prior iOS versions then you can use following code:

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];
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...