Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fcaldarelli/f6c841a19f56e792dc53105ea122f209 to your computer and use it in GitHub Desktop.
Save fcaldarelli/f6c841a19f56e792dc53105ea122f209 to your computer and use it in GitHub Desktop.
NSAttributes text from html and font
var text = "<span style='color:red'>text to check</span>"
let attrStr = try! NSMutableAttributedString(
data: text.data(using: .unicode, allowLossyConversion: true)!,
options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: String.Encoding.utf8.rawValue],
documentAttributes: nil)
let range = NSRange(location: 0, length: attrStr.length)
attrStr.addAttribute(NSFontAttributeName, value: font!, range: range)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment