Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 787
Kevlar Avatar asked Sep 10 '09 19:09

Kevlar


2 Answers

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

like image 73
mahboudz Avatar answered Nov 12 '22 06:11

mahboudz


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.

like image 3
Kevlar Avatar answered Nov 12 '22 06:11

Kevlar