Advertisement
Guest User

Untitled

a guest
May 19th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. @import CoreText;
  2.  
  3. @implementation NSAttributedString (Size)
  4.  
  5. - (CGFloat)heightForAttributedStringWithWidth:(CGFloat)inWidth {
  6. if (self.length == 0) {
  7. return 0;
  8. }
  9.  
  10. NSTextStorage *textStorage = [[NSTextStorage alloc]
  11. initWithAttributedString:self];
  12. NSTextContainer *textContainer = [[NSTextContainer alloc]
  13. initWithSize: CGSizeMake(inWidth, FLT_MAX)];
  14. NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
  15.  
  16. [layoutManager addTextContainer:textContainer];
  17. [textStorage addLayoutManager:layoutManager];
  18.  
  19. UIFont *font = [self attribute:NSFontAttributeName
  20. atIndex:0
  21. longestEffectiveRange:nil
  22. inRange:NSMakeRange(0, [textStorage length])];
  23.  
  24. [textStorage
  25. addAttribute:NSFontAttributeName value:font
  26. range:NSMakeRange(0, [textStorage length])];
  27.  
  28. [textContainer
  29. setLineFragmentPadding:0.0];
  30.  
  31. layoutManager.usesFontLeading = false;
  32.  
  33. [layoutManager
  34. ensureGlyphsForGlyphRange:NSMakeRange(0, [textStorage length])];
  35.  
  36. (void) [layoutManager
  37. glyphRangeForTextContainer:textContainer];
  38.  
  39. CGFloat height = [layoutManager
  40. usedRectForTextContainer:textContainer].size.height;
  41.  
  42. return height;
  43. }
  44.  
  45. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement