Skip to content

Instantly share code, notes, and snippets.

@fuxingloh
Last active October 28, 2023 22:36
Show Gist options
  • Star 22 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save fuxingloh/ccf26bb68f4b8e6cfd02 to your computer and use it in GitHub Desktop.
Save fuxingloh/ccf26bb68f4b8e6cfd02 to your computer and use it in GitHub Desktop.
iOS Swift: How to check if UILabel is truncated? Calculate number of lines for UILabel
func countLabelLines(label: UILabel) -> Int {
// Call self.layoutIfNeeded() if your view uses auto layout
let myText = label.text! as NSString
let rect = CGSize(width: label.bounds.width, height: CGFloat.greatestFiniteMagnitude)
let labelSize = myText.boundingRect(with: rect, options: .usesLineFragmentOrigin, attributes: [NSAttributedStringKey.font: label.font], context: nil)
return Int(ceil(CGFloat(labelSize.height) / label.font.lineHeight))
}
@emrdgrmnci
Copy link

I have 2 lines UILabels but still this extension shows me 1 line

@AbdElrahman-Rafaat-Amer
Copy link

I have 3 lines UILabels but still this extension shows me 1 line

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment