If you read either the documentation of sizeWithFont:forWidth:lineBreakMode:
or the header file you would have read that you should use boundingRectWithSize:options:attributes:context:
instead.
sizeWithFont is deprecated in iOS7 [duplicate]
-
30-06-2022 - |
Question
With the new iOS7 sizeWithFont:constrainedToSize:lineBreakMode
is deprecated and I receive warnings about it in my XCode 5. I have to say that is not affecting the functionality as far as I can tell but I would like to find an alternative to it in order to remove the annoying warnings. Here's my code related to the problem:
CGSize minimumLabelSize = [self.subLabel.text sizeWithFont:self.subLabel.font constrainedToSize:maxSize lineBreakMode:NSLineBreakByClipping];
and:
expectedLabelSize = [self.subLabel.text sizeWithFont:self.font constrainedToSize:maximumLabelSize lineBreakMode:NSLineBreakByClipping];
I wasn't able to figure it out by myself an solution and I don't know what to use instead.
Solution
OTHER TIPS
boundingRectWithSize:options:attributes:context: instead.
Just check Apple docs:
sizeWithFont:constrainedToSize:lineBreakMode:
Returns the size of the string if it were rendered with the specified constraints. (Deprecated in iOS 7.0. Use boundingRectWithSize:options:attributes:context: instead.)
-(CGSize) sizeWithFont2:(UIFont *)font
{
if ([self respondsToSelector:@selector(boundingRectWithSize:options:attributes:context:)])
{
CGSize result = [self sizeWithAttributes:@{NSFontAttributeName:font}];
return result;
}
return [self sizeWithFont:font]; //how to get rid warning here
}
- (CGSize) sizeWithFont2:(UIFont *)font constrainedToSize:(CGSize)size
{
if ([self respondsToSelector:@selector(boundingRectWithSize:options:attributes:context:)])
{
CGRect frame = [self boundingRectWithSize:size
options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading)
attributes:@{NSFontAttributeName:font}
context:nil];
return frame.size;
}
else
{
return [self sizeWithFont:font constrainedToSize:size]; //how to get rid warning here
}
}
Note: If they are perfectly equivalent why apple has to depreciate the old ones?
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow