Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate cell height based on cell's text in iOS 7

There are many solutions out there that use "sizeWithFont" or something similar, which happens to be deprecated as of iOS 7.

Here is some code I have pieced together so far. The height changes, but not at all accurately:

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

For another example, here's a similar answer: https://stackoverflow.com/a/9828777/693121 though as I said before, this utilizes deprecated code.

like image 220
Demasterpl Avatar asked Nov 25 '13 20:11

Demasterpl


2 Answers

You should use the method that's mentioned in the docs to replace the old one -- boundingRectWithSize:options:attributes:context:. Here's an example that I think should work (it works with multi-line labels anyway).

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

This assumes that you want the text view to be the size of self.view. If not, you should use initWithFrame for your text view, and pass calculationView.frame.size for the boundingRectWithSize: parameter.

like image 116
rdelmar Avatar answered Oct 18 '22 18:10

rdelmar


This here is the easiest version, iOS 7 does all the heavy lifting (only for autolayout-based 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 re-evaluates the autolayout constraints and returns the perfect height for your cell. Easy, right?

like image 41
Wirsing Avatar answered Oct 18 '22 19:10

Wirsing