Kevlar
Kevlar

Reputation: 8924

Why is sizeWithFont:constrainedToSize:lineBreakMode: returning an incorrect size?

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?

Upvotes: 5

Views: 5906

Answers (2)

Kevlar
Kevlar

Reputation: 8924

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.

Upvotes: 3

mahboudz
mahboudz

Reputation: 39376

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

Upvotes: 3

Related Questions