**I am trying to convert HTMLString :**
HTMLString在ios 11上没有转换为referencedstring但在ios10上运行得很好。这是HTMLString:
"<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\n<html>\n<head>\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n<meta http-equiv=\"Content-Style-Type\" content=\"text/css\">\n<title></title>\n<meta name=\"Generator\" content=\"Cocoa HTML Writer\">\n<style type=\"text/css\">\np.p1 {margin: 0.0px 0.0px 0.0px 0.0px; line-height: 26.0px; font: 16.0px \'.SF UI Text\'; color: #000000; -webkit-text-stroke: #000000}\nspan.s1 {font-family: \'.SFUIText-Semibold\'; font-weight: bold; font-style: normal; font-size: 16.00pt; font-kerning: none}\n</style>\n</head>\n<body>\n<p class=\"p1\"><span class=\"s1\">Hello</span></p>\n</body>\n</html>"
**the above HtmlString is not formatted with below code:**
func htmlToAttr(success: (_ status: NSAttributedString?) -> Void) -> Void {
let html = self //"<p>Hello <i>world</i>, hello</p>"
let data = html.data(using: .utf8)!
let att = try! NSAttributedString.init(
data: data, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
documentAttributes: nil)
let matt = NSMutableAttributedString(attributedString:att)
matt.enumerateAttribute(NSFontAttributeName,in:NSMakeRange(0,matt.length),
options:.longestEffectiveRangeNotRequired) { value, range, stop in
let f1 = value as! UIFont
let f2 = UIFont.systemFont(ofSize: 16) // UIFont(name:"SanFranciscoText", size: 16)
if let f3 = applyTraitsFromFont(f1, to:f2) {
matt.addAttribute(NSFontAttributeName, value:f3, range:range)
success(matt)
}
}
// return matt
}
func applyTraitsFromFont(_ f1: UIFont, to f2: UIFont) -> UIFont? {
let t = f1.fontDescriptor.symbolicTraits
if let fd = f2.fontDescriptor.withSymbolicTraits(t) {
return UIFont.init(descriptor: fd, size: 0)
}
return nil
}
以上代码转换Htmlstring tio字符串并在ios10上工作
Everything works great on iOS 10. Don't know what's wrong.