Skip to content

Instantly share code, notes, and snippets.

@cupnoodle
Created February 4, 2020 17:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cupnoodle/75fd46d4fd8af309027a3b67dbdd5b74 to your computer and use it in GitHub Desktop.
Save cupnoodle/75fd46d4fd8af309027a3b67dbdd5b74 to your computer and use it in GitHub Desktop.
Check if UILabel is truncated
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