I got an old project, and the code is using sizeWithFont. I got an warning from xcode saying it is first deprecated in iOS 7, and asked me to replace it with
(CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(NSDictionary *)attributes context:(NSStringDrawingContext *)context
I got two questions:
I. if I intend not to change it, what whould happen? Will it crash my app, or just bypass the deprecated API?
II. I wanted to use the suggested API, but I am confused it is asking for a CGSize paramter and returning a CGRect, while my old project just needs to return a CGSize. If I already got the CGSize, why I need the rect again? Please correct me and give code using the new API. Thanks a lot!
EDIT:
I have checked the answer in Replacement for deprecated sizeWithFont: in iOS 7?
I will do a self-answer to compare two solutions.
Another quesiton I have is:
I notice there is a [self setNumberOfLines:1];
, should I keep it or I can delete it? It does not impact anything in my code for now, but I don't know other situations, aka 'multiple line' situlation.
Old legacy code:
@implementation UILabel (dynamicSize)
-(CGFloat)expectedWidth{
[self setNumberOfLines:1];
CGSize maximumLabelSize = CGSizeMake(9999,self.frame.size.height);
CGSize expectedLabelSize = [[self text] sizeWithFont:[self font]
constrainedToSize:maximumLabelSize
lineBreakMode:[self lineBreakMode]];
return expectedLabelSize.width;
}
@end