Skip to content

Instantly share code, notes, and snippets.

@maninp
Forked from leapingbytes/NSString+LBToolbox.h
Last active August 29, 2015 14:24
Show Gist options
  • Save maninp/ef6ddbb753f6cd027337 to your computer and use it in GitHub Desktop.
Save maninp/ef6ddbb753f6cd027337 to your computer and use it in GitHub Desktop.
#import <Foundation/Foundation.h>
@interface NSString (LBToolbox)
- (CGSize) sizeWithWidth: (CGFloat) width andFont: (UIFont*) font;
@end
#import "NSString+LBToolbox.h"
#ifdef __IPHONE_6_0
@interface NSString (HappyARC)
- (CGRect) boundingRectWithSize: (CGSize) size options: (NSStringDrawingOptions) options attributes: (id) attributes context: (id) context;
@end
#endif
@implementation NSString (LBToolbox)
- (CGSize) sizeWithWidth: (CGFloat) width andFont: (UIFont*) font {
CGSize size = CGSizeMake( width, 2048);
CGSize result;
if ([self respondsToSelector:@selector(boundingRectWithSize:options:attributes:context:)]) {
NSDictionary* attributes = [NSDictionary dictionaryWithObjectsAndKeys: font, NSFontAttributeName, nil];
result = [self boundingRectWithSize: size options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context: nil].size;
}
else {
result = [self sizeWithFont: font constrainedToSize: size];
}
result.width = width;
return result;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment