This is it. This works with attributedText
, before falling back to plain text
, which makes a lot of sense for us folks who deal with multiple font families, sizes, and even NSTextAttachments!
Works fine with autolayout, but obviously the constraints must be defined and set before we check isTruncated
, otherwise the label itself wont even know how to lay itself out, so no way it would even know if its truncated.
It doesnt work to approach this problem with just a plain NSString
and sizeThatFits
. Im not sure how people were getting positive results like that. BTW, as mentioned numerous times, using sizeThatFits
is not ideal at all because it takes into account numberOfLines
for the resulting size, which defeats the whole purpose of what we are trying to do, because isTruncated
would always return false
regardless if its truncated or not.
extension UILabel {
var isTruncated: Bool {
layoutIfNeeded()
let rectBounds = CGSize(width: bounds.width, height: .greatestFiniteMagnitude)
var fullTextHeight: CGFloat?
if attributedText != nil {
fullTextHeight = attributedText?.boundingRect(with: rectBounds, options: .usesLineFragmentOrigin, context: nil).size.height
} else {
fullTextHeight = text?.boundingRect(with: rectBounds, options: .usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font: font], context: nil).size.height
}
return (fullTextHeight ?? 0) > bounds.size.height
}
}
UILabel
API. – Neomaneomah