Skip to content

Instantly share code, notes, and snippets.

@gali8
Created September 9, 2019 15:11
Show Gist options
  • Save gali8/45f5ce25ca111f549960e6b79f7ba2c1 to your computer and use it in GitHub Desktop.
Save gali8/45f5ce25ca111f549960e6b79f7ba2c1 to your computer and use it in GitHub Desktop.
import UIKit
extension String {
func html(font: UIFont) -> NSAttributedString? {
do {
let modifiedText = String(format: "<span style=\"font-family: \(font.fontName); font-size: \(font.pointSize)\">%@</span>", self) as String
let attrStr = try NSAttributedString(
data: modifiedText.data(using: String.Encoding.unicode, allowLossyConversion: true)!,
options: [.documentType: NSAttributedString.DocumentType.html, .characterEncoding: String.Encoding.utf8.rawValue],
documentAttributes: nil)
return attrStr
} catch let error {
debugPrint("label body error: \(error)")
return nil
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment