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

时间:2013-11-25 20:59:32

标签: ios objective-c uitableview ios7

有很多解决方案使用“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虽然如前所述,但它使用了弃用的代码。

5 个答案:

答案 0 :(得分:14)

您应该使用文档中提到的方法来替换旧的 - 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:参数传递calculateView.frame.size。

答案 1 :(得分:8)

这是最简单的版本,iOS 7完成了所有繁重的工作(仅适用于基于autolayout的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重新评估自动布局约束并返回单元格的完美高度。容易,对吧?

答案 2 :(得分:2)

我是从我的同事(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
}

答案 3 :(得分:0)

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

答案 4 :(得分:-1)

要获得字符串的大小,您应该使用- (CGSize)sizeWithAttributes:(NSDictionary *)attars

因此,对于您的示例,您将计算文本标签的高度:

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

CGSize *rowSize = [aString sizeWithAttributes:nil];