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

- (CGSize) sizeWithFont2:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(NSLineBreakMode) lineBreakMode
{
    return [self sizeWithFont2:font constrainedToSize:size]; //the NSLineBreakMode not used?

}

Notice that the code has 3 problems:

  1. For - (CGSize) sizeWithFont2:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(NSLineBreakMode) lineBreakMode, the parameter lineBreakMode is not used at all. I don't know how to use it in IOS 7. I look around in stackOverflow and the answers there also do not use that parameter.
  2. Also I think sizeWithFont: in IOS 6 must call sizeWithFont:constraintedToSize: but with a default size. But what would be the size?
  3. Finally I got warning in [self sizeWithFont:font]; because it's a deprecated function. I want to remove that warning.

Suggestions?

VMai
  • 10,156
  • 9
  • 25
  • 34
user4951
  • 32,206
  • 53
  • 172
  • 282

1 Answers1

2

Point 3 :

If you use SDK7 you will always have this warning or an error.

Point 2 :

You can choose your size like

CGSize maximumSize = CGSizeMake(300, FLT_MAX);

You can choose 300 for the width because it's the screen size with a little space for margins. But, you should use your own values for font and size.

Jordan Montel
  • 8,227
  • 2
  • 35
  • 40
  • Why not CGSizeMake(FLT_MAX, FLT_MAX); – user4951 Nov 04 '13 at 16:43
  • The best way is to get the width of your string using NSAttributedString. I do this to calculate the height of a text view here : http://stackoverflow.com/questions/19028743/ios7-uitextview-contentsize-height-alternative/19067476#19067476 – Jordan Montel Nov 04 '13 at 16:45