Last active
August 29, 2015 13:59
-
-
Save brennanMKE/10520304 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
+ (CGFloat)widthForAttributedString:(NSAttributedString *)text maxHeight:(CGFloat)maxHeight { | |
CGSize size = [self sizeForAttributedString:text maxSize:CGSizeMake(CGFLOAT_MAX, maxHeight)]; | |
return size.width; | |
} | |
+ (CGFloat)widthForString:(NSString *)text font:(UIFont *)font maxHeight:(CGFloat)maxHeight { | |
CGSize size = [self sizeForString:text font:font maxSize:CGSizeMake(CGFLOAT_MAX, maxHeight)]; | |
return size.width; | |
} | |
+ (CGFloat)heightForAttributedString:(NSAttributedString *)text maxWidth:(CGFloat)maxWidth { | |
CGSize size = [self sizeForAttributedString:text maxSize:CGSizeMake(maxWidth, CGFLOAT_MAX)]; | |
return size.height; | |
} | |
+ (CGFloat)heightForString:(NSString *)text font:(UIFont *)font maxWidth:(CGFloat)maxWidth { | |
CGSize size = [self sizeForString:text font:font maxSize:CGSizeMake(maxWidth, CGFLOAT_MAX)]; | |
return size.height; | |
} | |
+ (CGSize)sizeForAttributedString:(NSAttributedString *)text maxSize:(CGSize)maxSize { | |
if (![text isKindOfClass:[NSAttributedString class]] || !text.length) { | |
// no text means no height | |
return CGSizeMake(0.0f, 0.0f); | |
} | |
NSStringDrawingOptions options = NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading; | |
CGSize size = [text boundingRectWithSize:maxSize options:options context:nil].size; | |
CGSize paddedSize = CGSizeMake(size.width + 1, size.height + 1); | |
return paddedSize; | |
} | |
+ (CGSize)sizeForString:(NSString *)text font:(UIFont *)font maxSize:(CGSize)maxSize { | |
if (![text isKindOfClass:[NSString class]] || !text.length) { | |
// no text means no height | |
return CGSizeMake(0.0f, 0.0f); | |
} | |
NSStringDrawingOptions options = NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading; | |
NSDictionary *attributes = @{ NSFontAttributeName : font }; | |
CGSize size = [text boundingRectWithSize:maxSize options:options attributes:attributes context:nil].size; | |
CGSize paddedSize = CGSizeMake(size.width + 1, size.height + 1); | |
return paddedSize; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment