Skip to content

Instantly share code, notes, and snippets.

@cbess
Last active April 1, 2016 16:59
Show Gist options
  • Save cbess/6303604 to your computer and use it in GitHub Desktop.
Save cbess/6303604 to your computer and use it in GitHub Desktop.
Fix iOS [sizeWithFont:constrainedToSize:lineBreakMode:] deprecation warning. Adjusts the label height (top align text).
// adjust the label height (top align text)
// old
CGSize labelSize = [model.name sizeWithFont:self.nameLabel.font
constrainedToSize:_maxNameLabelSize
lineBreakMode:self.nameLabel.lineBreakMode];
// new
CGSize labelSize = [model.name boundingRectWithSize:_maxNameLabelSize
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSFontAttributeName: self.nameLabel.font}
context:nil].size;
// update label (won't compile, but you get it)
// self.nameLabel.frame.size.height = labelSize.height;
@fuxx
Copy link

fuxx commented Nov 18, 2013

And how do you set the lineBreakMode?

@furueili
Copy link

furueili commented Aug 7, 2014

NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;

[model.name boundingRectWithSize:_maxNameLabelSize
                         options:NSStringDrawingUsesLineFragmentOrigin
                      attributes:@{NSFontAttributeName: self.nameLabel.font,
                                   NSParagraphStyleAttributeName:paragraphStyle}
                         context:nil].size;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment