user_Dennis_Mostajo user_Dennis_Mostajo - 5 months ago 111
iOS Question

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

Answer

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