Create category that works for both iOS7 and iOS8 or only use sizeWithAttributes.
#import "NSString+StringSizeWithFont.h"
@implementation NSString (StringSizeWithFont)
- (CGSize) sizeWithMyFont:(UIFont *)fontToUse
{
if ([self respondsToSelector:@selector(sizeWithAttributes:)])
{
NSDictionary* attribs = @{NSFontAttributeName:fontToUse};
return ([self sizeWithAttributes:attribs]);
}
return ([self sizeWithFont:fontToUse]);
}
@end
Category call :
NSString *text= @"Do any additional setup after loading the view, typically from a nib.";
CGSize fontSize = [text sizeWithMyFont:[UIFont fontWithName:@"Helvetica"
size:12]];
NSLog(@"Calculate Size with sizeWithAttributes : %f %f", fontSize.height, fontSize.width);
CGSize fontSize1 = [text sizeWithFont:[UIFont fontWithName:@"Helvetica"
size:12]];
NSLog(@"Calculate Size with sizeWithFont: %f %f", fontSize1.height, fontSize1.width);
OutPut :
015-01-12 11:10:47.348 DemoTest[1149:38359] Calculate Size with sizeWithAttributes : 13.800000 350.871094
2015-01-12 11:10:47.349 DemoTest[1149:38359] Calculate Size with sizeWithFont: 14.000000 351.000000
hi
ReplyDeleteHello Vinus, Hope you are doing well. Let me know if you have any coding issue...
Delete