简体   繁体   中英

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.

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.

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?

I got it from my colleague (ANIRUDH) and working fine, may be helpful:

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

To get the size of your string you should use - (CGSize)sizeWithAttributes:(NSDictionary *)attars .

So for your example you would calculate the height of the text label as such:

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

or

CGSize *rowSize = [aString sizeWithAttributes:nil];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM