views:

2007

answers:

2

I'm having trouble displaying a multi-line UILabel in a custom UITableView cell.

I'm currently using this to calculate both the height of the cell...

NSString *cellText = [howtoSection objectAtIndex:row];
UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:15.0];
CGSize constraintSize = CGSizeMake(260.0f, MAXFLOAT);
CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];

return labelSize.height + 20;

...and this is for the label itself.

// Calc the height
NSString *cellText = [howtoSection objectAtIndex:row];
CGSize constraintSize = CGSizeMake(260.0f, MAXFLOAT);
CGSize labelSize = [cellText sizeWithFont:cell.textLabel.font constrainedToSize:constraintSize lineBreakMode:cell.textLabel.lineBreakMode];

// Create the label frame
CGRect newFrame = cell.contentLabel.frame;
newFrame.size.height = labelSize.height;
cell.contentLabel.frame = newFrame;

[cell.contentLabel setText:[howtoSection objectAtIndex:row]];

Everything is working as planned except that the label is being pushed down and out of it's cell. If it wasn't for this apparent top margin everything would fit.

Here's a link to a picture of what I'm seeing in the simulator...

iPhone Rendering Bug

Any help here would be greatly appreciated.

A: 

I don't see you setting cell.contentLabel.font in the code you show.

Also the label size calculation uses cell.textLabel.font to calculate it's size but renders using the contentLabel variable.

Is it possible you're rendering with a different font than the calculation?

CynicismRising
You were right on. I can't believe I screwed that up. Thanks for the help.
CrystalSkull
A: 

How are you adding your label to the contentView? It looks like the original positioning is wrong, since the height looks to be calculated correctly. If you comment out assigning the new frame, is the label in the correct position? My wager is that it isn't.

NilObject
I'm afraid I don't understand what you mean by adding it to the contentView? The custom cell was created in IB and I added the label that way.After commenting out the line that assigns the new frame all I see is the first line of my text in the correct position.
CrystalSkull
Ah, I'm sorry. I have always created my custom cells in code. Check the auto-resizing anchors to see if you're locked to the bottom, top, etc. You probably want it to be locked to the top of the cell.
NilObject