-1

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.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Daniela costina Vaduva
  • 1,857
  • 5
  • 20
  • 22
  • 1
    Have a look here : http://stackoverflow.com/questions/19028743/ios7-uitextview-contentsize-height-alternative/19067476#19067476 – Jordan Montel Oct 09 '13 at 15:20
  • 1
    A Google search with sizeWithFont deprecated iOS 7 gives a perfectly useful fix for your problem. Always Google first. – dandan78 Oct 09 '13 at 16:00
  • +1 and I want to know the same thing. The thing is I want to know the exact transformation from old code to the new code. The documentation doesn't give that. – user4951 Nov 04 '13 at 09:26

3 Answers3

3

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.

rckoenes
  • 69,092
  • 8
  • 134
  • 166
1
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.)

https://developer.apple.com/library/ios/documentation/uikit/reference/NSString_UIKit_Additions/DeprecationAppendix/AppendixADeprecatedAPI.html#//apple_ref/occ/instm/NSString/sizeWithFont:minFontSize:actualFontSize:forWidth:lineBreakMode:

Grzegorz Krukowski
  • 18,081
  • 5
  • 50
  • 71
1
-(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?

user4951
  • 32,206
  • 53
  • 172
  • 282