4

Need the required height of UITextView. sizeThatFits returns bigger, but the correct height than boundingRectWithSize. Why difference exist?

At two places I need to know the height. In cellForRowAtIndexPath and in heightForRowAtIndexPath.

I do not think it is efficient to create always a UITextView in heightForRowAtIndexPath just to know what height is required.

What workaround do you know to calculate height of a UITextView in heightForRowAtIndexPath?

János
  • 32,867
  • 38
  • 193
  • 353

2 Answers2

3

I met similar problem last month for UITableView, and I use boundingRectWithSize to calculate the size, it is actually correct. I then put it into UITextView.

Some mistakes I made:

  1. I forget to set the same font size when calculating and for UITextView
  2. UITextView has margins, I will manually add it in heightForRowAtIndexPath and set textContainerInset to the same one.

Hope it helps you.

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger section = indexPath.section;
    NSUInteger axisIndex = section - 2;
    yAxis *yAxisObj = self.yAxisInfoArray[axisIndex];
    boundingRect = [yAxisObj.yAxisDescription boundingRectWithSize:CGSizeMake(self.descriptionViewWidth, CGFLOAT_MAX)
                                                           options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading
                                                        attributes:@{NSFontAttributeName:self.contentFont}
                                                           context:nil];


    return boundingRect.size.height + TEXT_TOP_MARGIN + TEXT_BOTTOM_MARGIN;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellId = @"ChartDescriptionCell";
    ChartDescriptionCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[ChartDescriptionCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
        cell.textView.bounces = NO;
        cell.textView.showsHorizontalScrollIndicator = NO;
        cell.textView.showsVerticalScrollIndicator = NO;
        cell.textView.font = self.contentFont;
        cell.textView.textColor = [UIColor colorWithHex:@"#333333"];
        cell.textView.textContainerInset = UIEdgeInsetsMake(TEXT_TOP_MARGIN, -5, TEXT_BOTTOM_MARGIN, -5);
    }
    NSInteger section = indexPath.section;
    NSUInteger axisIndex = section - 2;
    yAxis *yAxisObj = self.yAxisInfoArray[axisIndex];
    cell.textView.text = yAxisObj.yAxisDescription;
    }
    return cell;
}
Wingzero
  • 9,644
  • 10
  • 39
  • 80
0

boundingRectWithSize returns size for text, so you should manually provide your font.

sizeThatFits returns size of UITextView with this text inside

If you are pointing to iOS 8 and above you can use Dynamic cell height which is very easy. In case of iOS 7 you need some workaround.

Tutorial: http://www.raywenderlich.com/87975/dynamic-table-view-cell-height-ios-8-swift

Related question with nice answer: Using Auto Layout in UITableView for dynamic cell layouts & variable row heights

Community
  • 1
  • 1
AlexZd
  • 2,152
  • 2
  • 18
  • 29
  • do you have any idea how to update `textView` height when content changes? http://stackoverflow.com/questions/32497229/how-update-height-of-the-cell-with-uitableviewautomaticdimension – János Sep 10 '15 at 08:52
  • @János try to use `textViewDidChange` delegate method and calculate new height each time. – AlexZd Sep 10 '15 at 10:18
  • @János I didn't understand you. You should to use constraints if you want dynamic UITableViewCell height. You can set for example 100 for UITextView and if text is bigger than 100 you need to update height constraint of UITextView – AlexZd Sep 10 '15 at 11:59
  • need constraints, but not height constraint, check image in this post: http://stackoverflow.com/questions/18746929/using-auto-layout-in-uitableview-for-dynamic-cell-layouts-variable-row-heights/18746930#18746930 – János Sep 10 '15 at 13:09