Advertisement
Guest User

Untitled

a guest
Dec 10th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1. extension UILabel {
  2. func getEstimatedHeight(width: CGFloat) -> CGFloat {
  3. guard let text = self.text else { return CGFloat.min }
  4.  
  5. var maxHeight = CGFloat.max
  6. if (self.numberOfLines > 0) {
  7. maxHeight = (ceil(self.font.lineHeight) * CGFloat(self.numberOfLines))
  8. }
  9.  
  10. let maxSize = CGSizeMake(width, maxHeight)
  11. let options = NSStringDrawingOptions.UsesFontLeading.union(.UsesLineFragmentOrigin)
  12. let estimatedSize = NSString(string: text).boundingRectWithSize(maxSize, options: options, attributes: [NSFontAttributeName: self.font], context: nil)
  13.  
  14. return estimatedSize.height
  15. }
  16. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement