Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @import CoreText;
- @implementation NSAttributedString (Size)
- - (CGFloat)heightForAttributedStringWithWidth:(CGFloat)inWidth {
- if (self.length == 0) {
- return 0;
- }
- NSTextStorage *textStorage = [[NSTextStorage alloc]
- initWithAttributedString:self];
- NSTextContainer *textContainer = [[NSTextContainer alloc]
- initWithSize: CGSizeMake(inWidth, FLT_MAX)];
- NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
- [layoutManager addTextContainer:textContainer];
- [textStorage addLayoutManager:layoutManager];
- UIFont *font = [self attribute:NSFontAttributeName
- atIndex:0
- longestEffectiveRange:nil
- inRange:NSMakeRange(0, [textStorage length])];
- [textStorage
- addAttribute:NSFontAttributeName value:font
- range:NSMakeRange(0, [textStorage length])];
- [textContainer
- setLineFragmentPadding:0.0];
- layoutManager.usesFontLeading = false;
- [layoutManager
- ensureGlyphsForGlyphRange:NSMakeRange(0, [textStorage length])];
- (void) [layoutManager
- glyphRangeForTextContainer:textContainer];
- CGFloat height = [layoutManager
- usedRectForTextContainer:textContainer].size.height;
- return height;
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement