Skip to content

Instantly share code, notes, and snippets.

@maxcampolo
Created August 29, 2016 18:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxcampolo/843221e19f1ca6f98f56ad4f4569f62b to your computer and use it in GitHub Desktop.
Save maxcampolo/843221e19f1ca6f98f56ad4f4569f62b to your computer and use it in GitHub Desktop.
Extension that calculates if a UILabel will be truncated depending on its number of lines
extension UILabel {
func willTruncate() -> Bool {
guard let labelText = text else {
return false
}
let mText = labelText as NSString
let attributes = [NSFontAttributeName : font]
let labelSize = mText.boundingRectWithSize(CGSize(width: bounds.width, height: CGFloat.max), options: .UsesLineFragmentOrigin, attributes: attributes, context: nil)
return Int(ceil(CGFloat(labelSize.height) / font.lineHeight)) > numberOfLines
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment