views:

374

answers:

2

I have two possible widths for a string i want to display in a label in a table cell, and i need to compute the height so that the table cell's height is recorded correctly. however, no matter what I do for the constraining size i get the same height, which is incorrect in the case i want. The code i'm using:

CGFloat width = 300.0f;
NSString * value = @"LongText LongText LongText LongText LongText LongText";
CGSize contentSize = [value sizeWithFont: [UIFont systemFontOfSize: 14.0f]
                       constrainedToSize: CGSizeMake(width, CGFLOAT_MAX)
                           lineBreakMode: UILineBreakModeWordWrap];

When i inspect the contentSize variable, the width is 252 and the height is 36 which is expected. But if instead of 300.0f i plug in 222.0f into the width variable, the width is 189 but the height is still 36, and only the first 4 LongText words are displayed on 2 lines (the third line seems to be cut off somehow in the calculation). Does anyone know why this is happening?

+2  A: 

Your code looks right. Look here and here to see if there is anything amiss in your code.

mahboudz
Thanks, but those don't really explain why the method isn't behaving correctly for thinner widths.
Kevlar
No, your code should be working. I thought maybe you'd see a difference between their code and yours... I didn't.
mahboudz
right; i'm just wondering if anyone else is aware that the apple code that actually calculates the size is not correct and if there is an alternative.
Kevlar
+1  A: 

It turns out it is returning the correct size; the code i was working on was using incorrect widths instead of the widths from this method which is why the text was cut off and i was confused.

Kevlar