Last active
April 1, 2016 16:59
-
-
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).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; |
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
And how do you set the lineBreakMode?