Brandon
Brandon

Reputation: 1751

Dynamic Row Height and sizeWithAttributes Warning

OK, im using the sizeWithFont: constrainedToSize capability, and I'm getting a compiler warning. I've read that I need to use sizeWithAttributes instead (link here: Replacement for deprecated sizeWithFont: in iOS 7?).

I'm having a very hard time understanding how to implement sizeWithAttributes dynamically, so that the height of my cell row changes to the content height (please see below).

Any ideas on how to get rid of this warning? Thanks!

int topPadding = cell.menuItemTitle.frame.origin.x;

int bottomPadding = cell.frame.size.height-
(topPadding+cell.menuItemTitle.frame.size.height);

NSString *text = [[menuItems objectAtIndex:indexPath.row] valueForKey:@"title"];

CGSize maximumSize = CGSizeMake(cell.menuItemTitle.frame.size.width, 9999);

CGSize expectedSize = [text sizeWithFont:cell.menuItemTitle.font 
constrainedToSize:maximumSize lineBreakMode:cell.menuItemTitle.lineBreakMode];

return topPadding+expectedSize.height+bottomPadding;

Upvotes: 0

Views: 82

Answers (1)

gabbler
gabbler

Reputation: 13766

Your topPadding should be origin.y instead of origin.x. You can use the following to get the size of the text.

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;

NSDictionary *attrDict = [NSDictionary dictionaryWithObjectsAndKeys:label.font,
                          NSFontAttributeName,
                          paragraphStyle,
                          NSParagraphStyleAttributeName,
                          nil];
CGRect expectedRect = [text boundingRectWithSize:maximumSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrDict context:nil];
CGSize expectedSize = expectedRect.size;

Upvotes: 1

Related Questions