Created
August 29, 2016 18:51
-
-
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
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
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