Dynamic height of cell with UILabel in tableview ios

13,266

Solution 1

Try this if you are working on above iOS 8. It works for me.

cell.messageLabel.text = [_messages objectAtIndex:indexPath.row];
cell.messageLabel.lineBreakMode = NSLineBreakByTruncatingTail;
cell.messageLabel.numberOfLines=0;
return cell;

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewAutomaticDimension;
}
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 10.0;
}

Solution 2

http://www.raywenderlich.com/73602/dynamic-table-view-cell-height-auto-layout

This is probably one of the best tutorials on the subject that I have come across. It explains everything step by step so you can adapt things to fit your need.

Solution 3

Try this:

- (float)getMessageSize:(NSString *)text Width:(float)textWidth
{
    NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:text
        attributes:@{
            NSFontAttributeName: [UIFont fontWithName:fontNameRefrig size:19.0]
        }];

    CGRect rect = [attributedText boundingRectWithSize:(CGSize){textWidth, CGFLOAT_MAX}
        options:NSStringDrawingUsesLineFragmentOrigin
        context:nil];
    CGSize finalSize = rect.size;
    return finalSize.height;
}
Share:
13,266
user2714823
Author by

user2714823

Updated on June 04, 2022

Comments

  • user2714823
    user2714823 almost 2 years

    I am working on a chat application which shows the message and image of user. I have a table view and a custom cell in it.The cell has a UIlabel and UIImage. I tried various methods to resize the cell height dynamically based on the content text of label .I have set the numberofLines=0 in the storyboard itself and set height of cell a constant so that multiple lines can fit in. But it does not show multiple lines and as for height I used auto dimension but it didn't work as well . I also used the following link And I am using a custom cell of type messageTableViewCell which has a property of label inside it.

    Here is the snapshot :-image

    My code in viewcontroller.m

    - (void)viewDidLoad
    {
    
       self.tableView.estimatedRowHeight = 100.0;
    
       self.tableView.rowHeight = UITableViewAutomaticDimension;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    
         messageTableViewCell *cell = [self.messageTableView dequeueReusableCellWithIdentifier:@"cell"];
    
        if (cell == nil) 
        {
             cell = [[messageTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        }
    
        cell.messageLabel.text = [_messages objectAtIndex:indexPath.row];
        cell.messageLabel.lineBreakMode = NSLineBreakByWordWrapping;
        cell.messageLabel.numberOfLines=0;
        [cell.messageLabel sizeToFit];
        return cell;
    }
    

    EDIT

    Instead of label I inserted a textview in the cell and modified it to :

    - (void)textViewDidChange:(UITextView *)textView
    {
        CGFloat fixedWidth = textView.frame.size.width;
        CGSize newSize = [textView sizeThatFits:CGSizeMake(fixedWidth,     MAXFLOAT)];
        CGRect newFrame = textView.frame;
        newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth),   newSize.height);
        textView.frame = newFrame;
    }
    
    
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath: (NSIndexPath *)indexPath
    {
        static messageTableViewCell *sizingCell = nil;
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            sizingCell = [self.messageTableView  dequeueReusableCellWithIdentifier:@"cell"];
       });
    
        sizingCell.textMessage.text=_messages[indexPath.row];
        CGFloat fixedWidth = sizingCell.textMessage.frame.size.width;
        CGSize newSize = [sizingCell.textMessage     sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)];
        CGRect newFrame = sizingCell.textMessage.frame;
        newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth), newSize.height);
        CGFloat height= newFrame.size.height;
        NSLog(@"%f is height ",height);
        return height+5.0f;
    }
    

    But it is also not working and it cut off the upper half of text.

  • user2714823
    user2714823 about 9 years
    Tried doing it but not helping :\
  • Numan Tariq
    Numan Tariq about 9 years
    It would be helpful to know what exactly is not working. Also, see the SO question that is linked by Koen in comment to your question. That is an excellent resource. The sample code in that one handles iOS 8 and 7 and you can combine them to support both.
  • JamEngulfer
    JamEngulfer over 8 years
    Just to note, for me the link to raywenderlich.com didn't work at all. Your method worked perfectly, with a little adjustment to take the additional height of the cell into account.
  • samthui7
    samthui7 over 7 years
    I did try solutions of Zure and Ray, but they not helped until your answer comes. Many thanks.