1

我有以下在 iOS6 中使用的方法,但在 iOS7 中出现错误

CGSize labelHeight = [tweetText sizeWithFont:[UIFont systemFontOfSize:13.0f] constrainedToSize:CGSizeMake(self.tweetsTableView.bounds.size.width - 84, 4000)];

下面的完整方法,关于如何修改iOS7的任何想法?

- (CGFloat)heightForCellAtIndex:(NSUInteger)index {

    NSDictionary *tweet = self.tweets[index];
    CGFloat cellHeight = 50;
    NSString *tweetText = tweet[@"text"];

    CGSize labelHeight = [tweetText sizeWithFont:[UIFont systemFontOfSize:13.0f] constrainedToSize:CGSizeMake(self.tweetsTableView.bounds.size.width - 84, 4000)];

    cellHeight += labelHeight.height;
    return cellHeight;
}
4

2 回答 2

1

我知道这是一个老问题和迟到的答案,但它仍然非常相关,

此 sizeWithFont 方法现已弃用,此新方法效果最佳

NSString *content = **Whatever your label's content is expected to be**
CGSize maximumLabelSize = CGSizeMake(390, 1000);

NSDictionary *stringAttributes = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:13] forKey: NSFontAttributeName];

CGSize newExpectedLabelSize = [content boundingRectWithSize:maximumLabelSize options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin attributes:stringAttributes context:nil].size;

因此,您可以将标签(或表格单元格等)调整为

label.frame.size.height = newExpectedLabelSize.height;

我希望这会有所帮助,干杯,吉姆。

于 2014-01-26T11:02:05.473 回答
0

添加此行:

UIFont *font = [UIFont boldSystemFontOfSize:16];
CGRect new = [string boundingRectWithSize:CGSizeMake(200, 300) options:NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName: font} context:nil];
CGSize stringSize= new.size;
于 2014-07-18T04:38:09.723 回答