Skip to content

Instantly share code, notes, and snippets.

@leapingbytes
Created August 17, 2013 19:08
Show Gist options
  • Save leapingbytes/6258306 to your computer and use it in GitHub Desktop.
Save leapingbytes/6258306 to your computer and use it in GitHub Desktop.
Way to create version proof code in ARC environment. (https://www.leapingbytes.com/blog/version_proofing_your_code_arc_twist)
#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