Calculating multiline text height for UILabel/UITableViewCell: different results when calculating vs actual drawing

41,173

Solution 1

Of course, the solution is obvious 30 seconds after posting. Maybe useful to others too...

The size by sizeWithFont: was correct. The sizes I calculated in the above way were incorrect, because [label sizeToFit] reduces the width of the label's frame. At subsequent calls to the same code, it started off with the frame that may already have been reduced.

The fix was to simply reset the frame width to a known good width before sizing to fit:

CGRect labelFrame = label.frame;
labelFrame.size.width = 310;
label.frame = labelFrame;
[label sizeToFit];

Solution 2

For multiline labels you need set

cell.textLabel.numberOfLines = 0;

and then

[cell.textLabel sizeToFit];

But for pretty view you need add some padding pixels. And your app will be awesome!

Solution 3

 titleSize = [title sizeWithFont:[UIFont systemFontOfSize:(CGFloat)17.0]
                                constrainedToSize:CGSizeMake(280, 2000)
                                    lineBreakMode:NSLineBreakByWordWrapping];
Share:
41,173
Jaanus
Author by

Jaanus

Design-driven engineer. iOS. Mac. Web frontend and some backend. Pixel pushing. Gestures, animations, transitions.

Updated on August 12, 2020

Comments

  • Jaanus
    Jaanus almost 4 years

    This general topic has been asked here multiple times: how to render UITableViewCells with varying amount of text and thus varying height. The canonical answer is: you calculate the height in table view controller delegate in heightForRowAtIndexPath using sizeWithFont:constrainedToSize:lineBreakMode:. Later, the cell gets drawn, and you use something like [label sizeToFit] if needed, and all works like magic.

    My problem: I am getting wrapping for some cells because sizeWithFont: returns different dimensions from actual drawing.

    A specific example:

    The text is this: "People forget that @BillGates had a sexy 1/4-inch thick slate back in 1993 from NEC. Whatever happens this week will NOT be about hardware!"

    CGSize theSize = [text sizeWithFont:[UIFont systemFontOfSize:17.0f] constrainedToSize:CGSizeMake(310.0f, FLT_MAX) lineBreakMode:UILineBreakModeWordWrap];
    NSLog(@"calculated size for %@: %f, %f",text, theSize.width, theSize.height);
    

    This returns: 306.000000, 84.000000. (I.e 4 rows with 17px font and 4px linespacing, 21px leading.) Good.

    However, later when actually drawing the cell:

    label = (UILabel *)[cell viewWithTag:3];
    label.text = [NSString stringWithFormat:@"%@", text];
    label.lineBreakMode = UILineBreakModeWordWrap;
    label.font = [UIFont systemFontOfSize:17.0f];
    CGSize labelSize;   
    labelSize = label.frame.size;
    NSLog(@"label size before resizing: %f, %f", labelSize.width, labelSize.height);
    [label sizeToFit];
    labelSize = label.frame.size;
    NSLog(@"label size after resizing: %f, %f for text %@", labelSize.width, labelSize.height,text);
    

    (UILabel is loaded as part of UITableViewCell from NIB. In IB I set it to 310px wide.)

    This should return exactly the same size as above. Instead, I get 281.000000, 105.000000 as the dimensions after sizeToFit call. It is now 5 lines at drawing time instead of 4, and the text spills over, I see the spillover in the UI.

    So, for the same text, I am getting two different dimensions calculated, and can't figure it out. Is it something about UILabel? Does it have some inner margins? This keeps happening for some texts but not others, and I have not traced it to something particular about the strings; seems random. This topic highlights that there are two processing passes: calculating height vs actual drawing. This is consistent with what I'm seeing. But I don't understand what exactly is going on or how to fix it.

    The question: why am I seeing two different calculated sizes, and how do I fix it?

  • cV2
    cV2 over 13 years
    thanks, that was what i was looking for: CGSize labelSize; labelSize = UILabelName.frame.size; ==> labelSize.height