根据iOS 7中的单元格文本计算单元格高度

有很多解决scheme使用“sizeWithFont”或类似的东西,这恰好在iOS 7中被弃用。

这是我迄今拼凑在一起的一些代码。 高度发生变化,但并不准确:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; // Configure the cell... cell.textLabel.text = "The Cell's Text!"; cell.textLabel.numberOfLines = 0; [cell.textLabel setLineBreakMode:NSLineBreakByWordWrapping]; [cell.textLabel sizeToFit]; return cell; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath; { CGRect screenBounds = [[UIScreen mainScreen] bounds]; CGSize screenSize = screenBounds.size; NSAttributedString *aString = [[NSAttributedString alloc] initWithString:"The Cell's Text!"]; UITextView *calculationView = [[UITextView alloc] init]; [calculationView setAttributedText:aString]; CGSize size = [calculationView sizeThatFits:CGSizeMake(screenSize.width, FLT_MAX)]; return size.height; } 

再举一个例子,这里有一个类似的回答: https : //stackoverflow.com/a/9828777/693121虽然正如我之前所说,这利用了不推荐使用的代码。

您应该使用文档中提到的方法replace旧的 – boundingRectWithSize:options:attributes:context :. 这里有一个我认为应该工作的例子(无论如何,它都可以和多行标签一起工作)。

 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { NSStringDrawingContext *ctx = [NSStringDrawingContext new]; NSAttributedString *aString = [[NSAttributedString alloc] initWithString:@"The Cell's Text!"]; UITextView *calculationView = [[UITextView alloc] init]; [calculationView setAttributedText:aString]; CGRect textRect = [calculationView.text boundingRectWithSize:self.view.frame.size options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:calculationView.font} context:ctx]; return textRect.size.height; } 

这假设你想要文本视图是self.view的大小。 如果不是,则应该使用initWithFrame作为文本视图,并为boundingRectWithSize:parameter passingcalculateView.frame.size。

这里是最简单的版本,iOS 7完成所有繁重的工作(仅适用于基于自动布局的uitableviewcells):

 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: @"Cell"]; [self configureCell:cell]; // edit: Need to call [cell layoutIfNeeded]; otherwise the layout changes wont be applied to the cell [cell layoutIfNeeded]; return [cell.contentView systemLayoutSizeFittingSize: UILayoutFittingCompressedSize].height; } 

systemLayoutSizeFittingSize重新评估自动布局约束并返回单元格的完美高度。 很简单,对吧?

我从我的同事(ANIRUDH)那里得知,工作正常,可能有帮助:

 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { //Calculating height on base of length of text UIFont *font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody]; NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:YOURSTRING //[NSString string withformat:@"\n Yourstring"] \n for: number of lines require for static label in cell attributes:@ { NSFontAttributeName: font }]; CGRect rect = [attributedText boundingRectWithSize:(CGSize){CELL_CONTENT_WIDTH, CGFLOAT_MAX} options:NSStringDrawingUsesLineFragmentOrigin context:nil]; CGSize size = rect.size; // NSString *height = [NSString stringWithFormat: @"%.2f", ceilf(size.height)+22]; return (ceilf(size.height)+22)*0.6; //change 0.6 to 0.9 according to the difference in required and extra calculted height, it works for me every time } 
 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *yourText = [[resultArray objectAtIndex:indexPath.row] valueForKey:@"review_text"]; CGSize lblwidth = CGSizeMake(300, CGFLOAT_MAX); CGSize requiredSize = [yourText sizeWithFont:[UIFont fontWithName:@"CALIBRI" size:17] constrainedToSize:lblwidth lineBreakMode:NSLineBreakByWordWrapping]; int calculatedHeight = requiredSize.height+50; return (float)calculatedHeight; } 

要获得你的string的大小,你应该使用- (CGSize)sizeWithAttributes:(NSDictionary *)attars

所以对于你的例子,你可以计算文本标签的高度:

 CGSize *cellSize = [cell.textLabel.text sizeWithAttributes:@{NSFontAttributeName:[cell.textLabel.font]}]; 

要么

 CGSize *rowSize = [aString sizeWithAttributes:nil];