1

I have my custom UITableViewCell with UITextView in it. After UITableView loads I need to adjust the size of the UITableViewCell with size of the text in a UITextView. So I am trying to it like this:

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

      resHeigth = MyCell.textView.contentSize.height;
      resHeigth += TOP_BOTTOM_MARGINS * 2;

      return resHeigth;
  }

But heightForRowAtIndexPath called before cellForRowAtIndexPath, where I actually set the text.

  MyViewCell *cell = (MyViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1];

    if (cell == nil) {

        [[NSBundle mainBundle] loadNibNamed:@"MyCellView" owner:self options:nil];
        cell = MyCell;
    }

    cell.textView.text  = @"Text for test\nTest";

    CGRect frame = cell.textView.frame;
    frame.size.height = cell.textView.contentSize.height;
    cell.textView.frame = frame;

So, Is it possible to change the height of the UITableViewCell?

ArisRS
  • 1,362
  • 2
  • 19
  • 41

2 Answers2

0

If you have the text available as NSString, then you can use NSString's instance method sizeWithFont:constrainedToSize:lineBreakMode: to measure the size that this string will take. Using that you can set the cell height in the heightForRowAtIndexPath method.

Update: In the constrainedToSize parameter you can set the height to a very high value (maybe int max, or screen height), so it returns full height that this string takes.

Mridul Kashatria
  • 4,157
  • 2
  • 18
  • 15
  • This method does't work correct for UITextView. It is only works for UILabel – ArisRS Jun 26 '11 at 17:07
  • I don't think there is a difference between UITextView and UILabel apart from former having a scroll container. The sizeWithFont method is generic and you should use the `sizeWithFont:forWidth:lineBreakMode:` method with a definite width. – Mridul Kashatria Jun 26 '11 at 17:13
  • Please look at that link http://stackoverflow.com/questions/2330939/sizewithfont-doesnt-give- correct-height-for-uitextview-if-there-is-a-long-string I've tried but it is not working – ArisRS Jun 26 '11 at 17:16
  • oops sorry I forgot that it works with **Single Line** only. Will update the answer. – Mridul Kashatria Jun 26 '11 at 17:19
0

Use the methods from NSString UIKit Additions to compute the size of the text.

CGSize sz = [yourText sizeWithFont:cellTextFont constrainedToSize:CGSizeMake(textFieldWidth,CGFLOAT_MAX);

Will return the size of the text when constrainted to a fixed size and uncontrainted vertically (CGFLOAT_MAX is the maximum value for floats)

AliSoftware
  • 32,623
  • 6
  • 82
  • 77
  • This method does't work correct for UITextView. It is only works for UILabel. But I need UITableView – ArisRS Jun 26 '11 at 17:08
  • It works exactly the same. As soon as you use the right method designed for multiline text (and not single line, see the documentation). UITextView renders the text the same way as UILabel, except that it is editable and is embed in a scrollview. – AliSoftware Jun 26 '11 at 17:18
  • I've just trying and it is not work correct. Here is another post about the problem: http://stackoverflow.com/questions/3141399/calculate-the-height-of-a-uitextviews-text-with-sizewithfontconstrainedtosizel If it possible, please create the sample project – ArisRS Jun 26 '11 at 17:43
  • No need to create a project, I do that all the time in my own projects. And the question you gave the link to… has the answer to your question too. – AliSoftware Jun 26 '11 at 17:47
  • So, As I understand I should add magic number to heightForRowAtIndexPath, in my case 16. But I think it is not good. Because for empty UITextView I am getting wrong value :( – ArisRS Jun 26 '11 at 19:40