2

Help me please to find an alternative for deprecated method...

CGSize size = [title sizeWithFont:buttonFont
                                  minFontSize:10
                               actualFontSize:nil
                                     forWidth:_view.bounds.size.width
                                lineBreakMode:NSLineBreakByClipping];

Can (boundingRectWithSize:options:attributes:context:) do this? Something like this...

    NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                [UIFont systemFontOfSize:10], NSFontAttributeName,
                                nil];

    CGSize size = [title boundingRectWithSize:CGSizeMake(_view.bounds.size.width-kBorder*2, _view.bounds.size.height)
                                      options:NSLineBreakByClipping
                                   attributes:attributes
                                      context:nil].size;

Am I right? Looking forward your advices :)

Konstantin
  • 156
  • 1
  • 9

1 Answers1

1

Have a look to a previous answer I made here using this code :

- (CGSize)text:(NSString *)text sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size
{
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(7.0))
    {
        NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                          font, NSFontAttributeName,
                                          nil];

        CGRect frame = [text boundingRectWithSize:size
                                          options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading)
                                       attributes:attributesDictionary
                                          context:nil];

        return frame.size;
    }
    else
    {
        return [text sizeWithFont:font constrainedToSize:size];
    }
}
Community
  • 1
  • 1
Jordan Montel
  • 8,227
  • 2
  • 35
  • 40
  • 1
    Weak-linking would be a better solution than explicit version check: `if ([text respondsToSelector:@selector(boundingRectWithSize:options:attributes:context:)])` – akashivskyy Oct 07 '13 at 14:49
  • @akashivskyy Ok thx, I will improve my answer so :) I'm curious, could you explain me why it is better please ? thanks – Jordan Montel Oct 07 '13 at 14:53
  • @JordanMontel It's always better to check for the specific API you need than checking the iOS version. – rmaddy Nov 13 '15 at 18:08