Replacement for deprecated -sizeWithFont:constrainedToSize:lineBreakMode: in iOS 7?

user_Dennis_Mostajo picture user_Dennis_Mostajo · Sep 19, 2013 · Viewed 72.6k times · Source

In iOS 7, the method:

- (CGSize)sizeWithFont:(UIFont *)font
     constrainedToSize:(CGSize)size
         lineBreakMode:(NSLineBreakMode)lineBreakMode 

and the method:

- (CGSize)sizeWithFont:(UIFont *)font

are deprecated. How can I replace

CGSize size = [string sizeWithFont:font
                 constrainedToSize:constrainSize
                     lineBreakMode:NSLineBreakByWordWrapping];

and:

CGSize size = [string sizeWithFont:font];

Answer

Xavier Maroñas picture Xavier Maroñas · Sep 23, 2013

You could try this:

CGRect textRect = [text boundingRectWithSize:size
                                 options:NSStringDrawingUsesLineFragmentOrigin
                              attributes:@{NSFontAttributeName:FONT}
                                 context:nil];

CGSize size = textRect.size;

Just change "FONT" for an "[UIFont font....]"