Created
May 17, 2018 15:46
-
-
Save fcaldarelli/f6c841a19f56e792dc53105ea122f209 to your computer and use it in GitHub Desktop.
NSAttributes text from html and font
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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