Is there any way to tell if UILabel has its text set using label.attributedText
or label.text
property?
The problem is when you set attributedText
, text
is also updated and vice versa, so it is not possible to check these properties for nil.
You are dividing width by height and if it is bigger than number of line (which might very well be 0) you are saying label is truncated.
The styled text that the text view displays.
A view that displays one or more lines of informational text.
Inspired by @lukas-o I have written an extension on UILabel
that determines if it contains an attributedText
or not. In fact if the NSAttributedString
does not contain any attributes, this computed property will evaluate it to not be attributed.
extension UILabel {
var isAttributed: Bool {
guard let attributedText = attributedText else { return false }
let range = NSMakeRange(0, attributedText.length)
var allAttributes = [Dictionary<String, Any>]()
attributedText.enumerateAttributes(in: range, options: []) { attributes, _, _ in
allAttributes.append(attributes)
}
return allAttributes.count > 1
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With