sizeWithFont: ConstrainedToSize: lineBreakMode: method is deprecated in iOS 7 and I'm a little unsure how to handle this exactly. After a little of research on the internet I found that there's a new method for handling this, which is:
[txt drawWithRect: options: attributes: context:]
This is the method that I am currently attempting to run:
+ (CGSize)textSizeForText:(NSString *)txt
{
CGFloat width = [UIScreen mainScreen].applicationFrame.size.width * 0.75f;
CGFloat height = MAX([JSBubbleView numberOfLinesForMessage:txt],
[txt numberOfLines]) * [JSMessageInputView textViewLineHeight];
return [txt sizeWithFont:[JSBubbleView font]
constrainedToSize:CGSizeMake(width - kJSAvatarSize, height + kJSAvatarSize)
lineBreakMode:NSLineBreakByWordWrapping];
}
But I'm having a hard time converting it to the new method. Mainly with the lineBreakMode: which is nowhere in the new method. Any ideas?
in the new method, for line break, you have to create a NSMutableParagraphStyle Style first:
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
Then simply use the new method by passing all the necessary parameters
CGRect textRect = [text boundingRectWithSize:CGSizeMake(width - kJSAvatarSize, height + kJSAvatarSize)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSParagraphStyleAttributeName: paragraphStyle.copy}
context:nil];
return textRect.size;
if you want this to be tide, you can do
return ([text boundingRectWithSize:CGSizeMake(width - kJSAvatarSize, height + kJSAvatarSize)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSParagraphStyleAttributeName: paragraphStyle.copy}
context:nil]).size;
Hope that helps
I will add the font attributes in above answer
return ([text boundingRectWithSize:CGSizeMake(width - kJSAvatarSize, height + kJSAvatarSize)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:@{NSParagraphStyleAttributeName: paragraphStyle.copy, NSFontAttributeName:[UIFont fontWithName:@"HelveticaNeue" size:14]}
context:nil]).size;
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments