Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Build a Diary App Using Core Data Custom UITableViewCell Customizing Table View Cell Appearance

Gaetano Isidori
Gaetano Isidori
3,451 Points

How do i get the body text to adjust to the container?

Im having trouble getting the text in the body to be closer to the date label and getting the body label to stretch with more text than a regular diary, ive tried messing around with the configureCellForEntry margins and what not but i cant get it right, can anyone help?

  • (CGFloat)heightForEntry:(DiaryEntry *)entry{ const CGFloat topMargin = 35.0f; const CGFloat bottomMargin = 60.0f; const CGFloat minHeight = 85.0f; UIFont *font = [UIFont systemFontOfSize:[UIFont systemFontSize]];

    CGRect boundingBox = [entry.body boundingRectWithSize:CGSizeMake(202, CGFLOAT_MAX) options:(NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin) attributes:@{NSFontAttributeName: font} context:nil];

    return MAX(minHeight, CGRectGetHeight(boundingBox) + topMargin + bottomMargin); }

Eric M
Eric M
2,242 Points

Change this to log out the height. If you're like me, you'll see the height calculated is actually correct:

CGFloat height = MAX(minHeight, CGRectGetHeight(boundingBox) + topMargin +     bottomMargin);
NSLog(@"%f px", height);
return height;

I think I have the same problem as you. Height calculated is correct but body text isn't following the container correctly.

Do you also have an inner view inside the table cell prototype, that then contains the labels and images?