Inspired by @ lukas-o I wrote an extension on UILabel that determines whether it contains attributedText or not. In fact, if the NSAttributedString does not contain any attributes, this computed property will evaluate it so that it is not assigned.
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 } }
source share