// NSString+sizeWithFont : iOS7で非推奨となったNSString:sizeWithFont系メソッドの代替メソッド
// http://ios.ch3cooh.jp/entry/20140424/1398309284 を元にiOS6以下対応を追加

#import "NSString+sizeWithFont.h"

@implementation NSString (sizeWithFont)

- (CGSize)sizeWithFont7:(UIFont *)font
{
#if !__IPHONE_OS_VERSION_MIN_REQUIRED>=70000	// iOS7専用ではないとき
    if ([self respondsToSelector:@selector(boundingRectWithSize:options:attributes:context:)]) {
#endif
		// iOS7
	   	return [self sizeWithAttributes:@{ NSFontAttributeName:font }];
#if !__IPHONE_OS_VERSION_MIN_REQUIRED>=70000	// iOS7専用ではないとき
	}
	// iOS5/6専用
	return [self sizeWithFont:font];
#endif
}

- (CGSize)sizeWithFont7:(UIFont *)font constrainedToSize:(CGSize)size
{
#if !__IPHONE_OS_VERSION_MIN_REQUIRED>=70000	// iOS7専用ではないとき
    if ([self respondsToSelector:@selector(boundingRectWithSize:options:attributes:context:)]) {
#endif
		// iOS7
    	return [self sizeWithFont7:font constrainedToSize:size lineBreakMode:NSLineBreakByWordWrapping];
#if !__IPHONE_OS_VERSION_MIN_REQUIRED>=70000	// iOS7専用ではないとき
    }
	// iOS5/6
	return [self sizeWithFont:font constrainedToSize:size];
#endif
}

- (CGSize)sizeWithFont7:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(NSLineBreakMode)lineBreakMode
{
#if !__IPHONE_OS_VERSION_MIN_REQUIRED>=70000	// iOS7専用ではないとき
    if ([self respondsToSelector:@selector(boundingRectWithSize:options:attributes:context:)]) {
#endif
		// iOS7
	    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
	    style.lineBreakMode = lineBreakMode;
	    style.alignment 	= NSTextAlignmentLeft;
	    NSDictionary *attributes = @{
	                                 NSFontAttributeName : font,
	                                 NSParagraphStyleAttributeName : style
	    };
	    CGRect rect = [self boundingRectWithSize:size
	                                     options:NSStringDrawingUsesLineFragmentOrigin
	                                  attributes:attributes
	                                     context:nil];
	    return rect.size;
#if !__IPHONE_OS_VERSION_MIN_REQUIRED>=70000	// iOS7専用ではないとき
	}
	// iOS5/6
	return [self sizeWithFont:font constrainedToSize:size lineBreakMode:lineBreakMode];
#endif
}

@end
