7

I have a UITableView (Grouped!) and need to calculate the height of two styles of cells: UITableViewCellStyleDefault and UITableViewCellStyleValue2.

This is how I do it for UITableViewCellStyleDefault:

CGSize  textSize = {300.f, 200000.0f};
CGSize  size = [myTextString1 sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:textSize lineBreakMode:UILineBreakModeWordWrap];
size.height += 30.0f;
result = MAX(size.height, 44.0f);

And for UITableViewCellStyleValue2:

CGSize  textSize = {207.f, 200000.0f};
CGSize  size = [myTextString2 sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:textSize lineBreakMode:UILineBreakModeWordWrap];
size.height += 30.0f;
result = MAX(size.height, 44.0f);

My issue it that they return incorrect heights and I think it's the textSize where I use incorrect numbers. With long texts, the bottom part gets cut short (usually just a line with a few words), and for both CellStyles they have weird spacing above and below the text in the cell.

For UITableViewCellStyleValue2 I got the width size (207.0f) from making the text.backgroundColor red and then just calculating the size of the box. 300.0f width for UITableViewCellStyleDefault is how big the cell is with UITableViewStyleGrouped.

Does anyone know which values I need to use to properly calculate the size of an NSString and thereby get appropriate height for my cells?

Thanks

6 Answers 6

9

Here is the code I am using for this. It works like a charm for one type of cell. It may have some useful parts for your application.

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath: (NSIndexPath *) indexPath {
AppAppDelegate *appDelegate = (AppAppDelegate *)[[UIApplication sharedApplication] delegate];
NSString *Text = ([[appDelegate.myTextSectionsDelegateDict objectAtIndex:indexPath.section] objectForKey:@"Text"]);

    UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:17.0];
    CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
    CGSize labelSize = [Text sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
    return labelSize.height + 15;}
1
  • 1
    Thanks. I ended up doing something alone with same lines. I found out I was having issues with the Value2 cells, because I assumed they were fontSize 14, while they're actually 15. For your code, you might want to add: result = MAX(labelsize.height + 15, 44.0f); Just in case if the cell is ever empty or less then 2 lines it'll return it with a minimum row height of 44.0f, which is the normal. If there's just one line it'll make it smaller than 44.0f.
    – runmad
    Jul 27, 2009 at 19:40
2

When you create your cell, are you using the same font as the one you use to measure?

I used the tutorial on this page and everything worked for me. It might be useful to you too:

2
  • Yes, I use the same font size and type (bold/not bold). I'll have a look at the link and see if that solves it and let you know.
    – runmad
    Jul 21, 2009 at 19:00
  • I found out I wasn't using the correct font! I found out I was having issues with the Value2 cells, because I assumed they were fontSize 14, while they're actually 15. The other cell style I needed to play around with the amount of height it adds to the text height.
    – runmad
    Jul 27, 2009 at 19:41
2

It seeems you've figured out that you were using the wrong text size. You might want to just use the font and lineBreakMode properties of the label to avoid this problem in the future, especially if you change them in the cell at a later time. Also, for readability's sake, I would avoid adding to the height before returning a number. Instead I'd try something like this:

CGSize textSize = CGSizeMake( 300.0, 1979 );
CGSize size = [ myTextString1 sizeWithFont:[[ cell textLabel ] font ]
                         constrainedToSize:textSize
                             lineBreakMode:[[ cell textLabel ] lineBreakMode ]];

result = MAX( size.height + 30, 44.0f );

I used 1979 because according to the documentation, one should not return values larger than 2009.

1
  • Thanks. Yeah, I set some constants for the font sizes, so if I change it for the cell, it'll update everywhere.
    – runmad
    Jul 30, 2009 at 16:50
2

If you want to calculate the height properly for cellvalue2 you should use: [UIFont boldSystemFontOfSize:15.0f] and as linebreakmode: NSLineBreakByTruncatingTail

If you don't use bold, the text will fill correctly but you will lose the correct vertical padding.

The rest of your calculation are correct.

0

When is working for me is to compute the constraintSize based on the width of the table view:

CGSize constraintSize = CGSizeMake(tableView.frame.size.width * 0.6, 2009);
CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
0

Easiest way to do this:

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

CGFloat result = 10.0f;
if(indexPath.section == 1) // here i compare the sections
{
    result = 400.0f;
}
return result;
}

Return the values based on the sections you have. This will give you custom row heights.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.