Created
February 4, 2020 17:26
-
-
Save cupnoodle/75fd46d4fd8af309027a3b67dbdd5b74 to your computer and use it in GitHub Desktop.
Check if UILabel is truncated
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 { | |
var isTruncated: Bool { | |
guard let labelText = text else { | |
return false | |
} | |
let labelTextSize = (labelText as NSString).boundingRect( | |
with: CGSize(width: frame.size.width, height: .greatestFiniteMagnitude), | |
options: .usesLineFragmentOrigin, | |
attributes: [.font: font!], | |
context: nil).size | |
// check if the text height is taller than the label frame height | |
// if yes, means its truncated | |
return labelTextSize.height > bounds.size.height | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment