0

I'm Trying to change the UITextView height inside a uitableviewcell, i have search a lot on SO and there are a lot of answer, many of which are very similar. I think the best answer i have found is this:

UITableViewCell with UITextView height in iOS 7?

i have tried, but doesn't work very well, this i my code:

- (CGFloat)textViewHeightForRowAtIndexPath: (NSIndexPath*)indexPath {
    UITextView *calculationView = [self.textViews objectForKey: indexPath];
    CGFloat textViewWidth;
    if (!calculationView.attributedText) {
        // This will be needed on load, when the text view is not inited yet

        calculationView = [[UITextView alloc] init];
        calculationView.attributedText = [[NSAttributedString alloc] initWithString:[[self.comments objectAtIndex:indexPath.row] valueForKey:@"comment"] attributes:self.regularDict];// get the text from your datasource add attributes and insert here
        textViewWidth = 250; // Insert the width of your UITextViews or include calculations to set it accordingly
    } else {
        textViewWidth = calculationView.frame.size.width;
    }
    CGSize size = [calculationView sizeThatFits:CGSizeMake(textViewWidth, FLT_MAX)];
    return size.height;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

return [self textViewHeightForRowAtIndexPath:indexPath];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CommentCell *cell = (CommentCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    NSAttributedString *text_user = [[NSAttributedString alloc] initWithString:[[self.comments objectAtIndex:indexPath.row] valueForKey:@"comment"] attributes:self.regularDict];

    [cell.my_text setAttributedText:text_user];
    [cell.my_text setTextAlignment:NSTextAlignmentRight];

    CGRect frame = cell.my_text.frame;

        CGSize size = [cell.my_text sizeThatFits:CGSizeMake(250, FLT_MAX)];

frame.size.height = size.height;

    [cell.my_text setFrame:frame];
    [self.textViews setObject:cell.my_text forKey:indexPath];

    return cell;
}

but this is the result:

enter image description here

there is a lot of empty space in the row, i can't understand why...

anyone can help me?

Community
  • 1
  • 1
Piero
  • 9,173
  • 18
  • 90
  • 160

2 Answers2

0

More information on what you are attempting to achieve would be helpful, however...

I think you may find that tableView:heightForRowAtIndexPath: runs to completion prior to tableView:cellForRowAtIndexPath:, so any code to change the height of the UITextView within tableView:(UITableView *)tableView cellForRowAtIndexPath: will not be effective.

To be clear, all the row heights are set prior to UITableView filling in the contents of the cell within that row.

So when using tableView:heightForRowAtIndexPath:, it is important to establish the height of each cell at indexPath within this method.

andrewbuilder
  • 3,629
  • 2
  • 24
  • 46
0

i had this issue once, it ended up to be because of the font i used didnt require as much height as "sizeThatFits" returned. since then im carrying the following helping method with me whenever i need it:

- (CGFloat)heightOfTextViewWithString:(NSString *)string 
                             withFont:(UIFont *)font 
                        andFixedWidth:(CGFloat)fixedWidth
{
    UITextView *tempTV = [[UITextView alloc]initWithFrame:CGRectMake(0, 0, fixedWidth, 1)];
    tempTV.text = [string uppercaseString];
    tempTV.font = font;

    CGSize newSize = [tempTV sizeThatFits:CGSizeMake(fixedWidth, MAXFLOAT)];
    CGRect newFrame = tempTV.frame;
    newFrame.size = CGSizeMake(fmaxf(newSize.width, fixedWidth), newSize.height);
    tempTV.frame = newFrame;

    return tempTV.frame.size.height;
}
carlos16196
  • 706
  • 1
  • 7
  • 10