I know that this will look little over the top but ... This code will add html support to UILabel, UITextView, UIButton and you can easily add this support to any view that has attributed string support :
public protocol CSHasAttributedTextProtocol: AnyObject {
func attributedText() -> NSAttributedString?
func attributed(text: NSAttributedString?) -> Self
}
extension UIButton: CSHasAttributedTextProtocol {
public func attributedText() -> NSAttributedString? {
attributedTitle(for: .normal)
}
public func attributed(text: NSAttributedString?) -> Self {
setAttributedTitle(text, for: .normal); return self
}
}
extension UITextView: CSHasAttributedTextProtocol {
public func attributedText() -> NSAttributedString? { attributedText }
public func attributed(text: NSAttributedString?) -> Self { attributedText = text; return self }
}
extension UILabel: CSHasAttributedTextProtocol {
public func attributedText() -> NSAttributedString? { attributedText }
public func attributed(text: NSAttributedString?) -> Self { attributedText = text; return self }
}
public extension CSHasAttributedTextProtocol
where Self: CSHasFontProtocol, Self: CSHasTextColorProtocol {
@discardableResult
func html(_ text: String) -> Self { html(text: text) }
@discardableResult
func html(text: String) -> Self {
let html = """
<html><body style="color:\(textColor!.hexValue()!);
font-family:\(font()!.fontName);
font-size:\(font()!.pointSize);">\(text)</body></html>
"""
html.data(using: .unicode, allowLossyConversion: true).notNil { data in
attributed(text: try? NSAttributedString(data: data, options: [
.documentType: NSAttributedString.DocumentType.html,
.characterEncoding: NSNumber(value: String.Encoding.utf8.rawValue)
], documentAttributes: nil))
}
return self
}
}
public protocol CSHasFontProtocol: AnyObject {
func font() -> UIFont?
func font(_ font: UIFont?) -> Self
}
extension UIButton: CSHasFontProtocol {
public func font() -> UIFont? { titleLabel?.font }
public func font(_ font: UIFont?) -> Self { titleLabel?.font = font; return self }
}
extension UITextView: CSHasFontProtocol {
public func font() -> UIFont? { font }
public func font(_ font: UIFont?) -> Self { self.font = font; return self }
}
extension UILabel: CSHasFontProtocol {
public func font() -> UIFont? { font }
public func font(_ font: UIFont?) -> Self { self.font = font; return self }
}
public protocol CSHasTextColorProtocol: AnyObject {
func textColor() -> UIColor?
func text(color: UIColor?) -> Self
}
extension UIButton: CSHasTextColorProtocol {
public func textColor() -> UIColor? { titleColor(for: .normal) }
public func text(color: UIColor?) -> Self { setTitleColor(color, for: .normal); return self }
}
extension UITextView: CSHasTextColorProtocol {
public func textColor() -> UIColor? { textColor }
public func text(color: UIColor?) -> Self { textColor = color; return self }
}
extension UILabel: CSHasTextColorProtocol {
public func textColor() -> UIColor? { textColor }
public func text(color: UIColor?) -> Self { textColor = color; return self }
}