0

Hi All I'm building a chat function on my app.

In each of my custom table cells I have a UITextView which contains the users comments.

I am trying to Adjust the size of the UITextView depending on the content and then also adjust the height of the cell.

The first problem I have run into is when I try and resize the UITextView with sizeToFit. for some reason it makes the width of the UITextView very sometimes very narrow and often doesn't work at all.

Here is the code I have so far:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"ClubChatCell";
    ClubChatCell *cell = (ClubChatCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];


    if (cell == nil) {
        cell = [[ClubChatCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    ClubDetails *club = nil;

    club = [_clubs objectAtIndex:indexPath.row];

    [cell.commentText setScrollEnabled:YES];
    cell.commentText.text = club.comment;
    [cell.commentText sizeToFit];
    [cell.commentText setScrollEnabled:NO];

    cell.fullNameLabel.text = club.creator;

    cell.profilePic.image = club.creatorProfilePic;
    cell.profilePic.layer.cornerRadius = cell.profilePic.frame.size.height /2;
    cell.profilePic.layer.masksToBounds = YES;
    cell.profilePic.layer.borderWidth = 0;
    cell.timeLabel.text = club.commentDate;



    return cell;
}

Then for the height of the TableViewCell:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ClubDetails *club = nil;
    club = [_clubs objectAtIndex:indexPath.row];

    NSString *cellText = club.comment;
    UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:14.0];
    CGSize constraintSize = CGSizeMake(225.0f, MAXFLOAT);
    CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];

    return labelSize.height + 80;
}

Can someone just point me in the right direction.

Khledon
  • 193
  • 5
  • 23

2 Answers2

0

Try to find solution here - How do I size a UITextView to its content?.

There is solution :

 CGRect frame = _textView.frame;
frame.size.height = _textView.contentSize.height;
_textView.frame = frame;

For my app i used this solution:

- (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;
}
Community
  • 1
  • 1
Evgeniy Kleban
  • 6,794
  • 13
  • 54
  • 107
0

I just recently had this problem and was able to figure it out by using boundingRectWithSize:options:attributes:context

This method takes an NSString's text and returns the CGRect that would fit that text with the supplied attributes.

You could use it similarly to this:

NSString *text = @"This is my text. It could be any length";
CGSize maxLabelSize = CGSizeMake(280, FLT_MAX); 
CGRect rectForTextView = [text boundingRectWithSize:maxLabel Sizeoptions:NSStringDrawingUsesLineFragmentOrigin attributes:textAttributes context:nil];

The CGSize variable is what the constrains should be for your CGRect/TextView. In this case I made it so that its width will be a max of 280 and the height will be the maximum float value. Having the height as a max value will allow it expand almost indefinitely. You can obviously set those values to whatever you want, though.

The next thing to do will be to take the height of the CGRect 'rectForTextView' and assign it to a CGFloat.

CGFloat cellHeight = CGRectGetHeight(rectForTextView); 

The height that is now assigned to the cellHeight variable is what you would want to set your tableView's row height and textView height at.

Keep in mind that you might want to add some extra margin space for the text because it will literally be the exact height that is required to constrain the given text.

I created a basic class method that performs this operation so that I can easily reuse it in my tableView:heightForRowAtIndexPath: method.

BlueBear
  • 7,469
  • 6
  • 32
  • 47
  • Thanks for your reply. But I'm still having issues with this! My UITextView doesn't adjust to the size of the text until I scroll the table. Then when I do Scroll some funny stuff starts happening - Some of the text is being wrapped effectively, so of it is still being cut of and all the text is being populated in the top left of my Cell. Any Ideas? – Khledon Apr 19 '14 at 10:22