views:

2745

answers:

3

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

+2  A: 

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:

rein
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.
Canada Dev
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.
Canada Dev
+2  A: 

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;}
Jonah
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.
Canada Dev
+1  A: 

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.

Jeff Kelley
Thanks. Yeah, I set some constants for the font sizes, so if I change it for the cell, it'll update everywhere.
Canada Dev