iosiphoneios7nsstringsizewithfont

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


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];

Solution

  • 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....]"