versuchen Sie es mit NSAttributedString mit NSHTMLTextDocumentType Option und .utf8 String-Codierung
Swift 3:
extension String {
var htmlAttributedString: NSAttributedString? {
do {
return try NSAttributedString(data: data(using: .utf8)!, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: String.Encoding.utf8.rawValue], documentAttributes: nil)
} catch let error {
print(error.localizedDescription)
}
return nil
}
}
Verwenden
let htmlStringCode = "<i>Italics </i><b>Bold </b><u>Underline</u><blockquote>block quote<br></blockquote>"
if let htmlText = htmlStringCode.htmlAttributedString {
let attributes = [NSFontAttributeName: UIFont(name: "Open Sans", size: 18.0)!]
let attributedText = NSMutableAttributedString(string: htmlText.string, attributes: attributes)
let htmlString = htmlText.string as NSString
let range = htmlString.range(of: "Italics")
attributedText.addAttribute(NSFontAttributeName, value: UIFont(name: "OpenSans-Italic", size: 18.0)!, range: range)
textView.attributedText = attributedText
}
ich versuchen würde, einen 'NSAttributedString' mit der' NSHTMLTextDocumentType' Option zu konstruieren und ändern dann die Attribute die gewünschte Schriftart auf jeder seinen Teil zu erzwingen. – werediver