I need to set two attributes in the text represented UILabel: the distance between the letters (kern) and its strikethrough style. Based on the documentation NSAttributedStringKey, I created the following extension for UILabel:
extension UILabel {
func setStrikeThroughSpacedText(text: String, kern: CGFloat?) {
var attributes: [NSAttributedStringKey : Any] = [:]
if let kern = kern {
attributes[.kern] = kern
}
attributes[.strikethroughStyle]
= NSNumber(integerLiteral: NSUnderlineStyle.styleSingle.rawValue)
self.attributedText = NSAttributedString(string: text,
attributes: attributes)
}
}
However, it seems that the key .kernsomehow collides with the key .strikethroughStyle, because if I specify kern, the kernel is applied, but not the strikethrough style. If I don't specify kern (so the extension does not apply the attribute .kern), the strikethrough style works.
Anyone have another way how to get around this error (I assume this is an error)?
source
share