2

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?

  • possible duplicate of [deprecated in iOS 7 " sizeWithFont: constrainedToSize: lineBreakMode: " how can I replacement?](http://stackoverflow.com/questions/18903304/deprecated-in-ios-7-sizewithfont-constrainedtosize-linebreakmode-how-can) – bbarnhart Feb 09 '14 at 03:17

1 Answers1

13

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;
Meenakshi
  • 259
  • 5
  • 19
Xu Yin
  • 3,932
  • 1
  • 26
  • 46
  • Hey thanks for the answer, I will just add font attribute along with lineBreakMode – Meenakshi May 09 '14 at 13:09
  • What exactly is paragraphStyle? That variable is absent in the deprecated code, then, what value should it have? – Josh Oct 06 '16 at 08:01