我目前正在使用此answer提供的此扩展程序将html转换为UITextView
内的字符串。一切都很完美,但由于某些奇怪的原因,NSFontAttributeName
在此过程中未应用于字符串。我在这里做错了吗? (或者我应该在使用归属的html解析字符串后应用NSAttributedString
吗?如果是,是否可以将属性应用于NSAttributeString
?)。
PS。使用html2String
时字体大小不会更改,但html中的链接会被UITextView
extension String {
var html2AttributedString: NSAttributedString? {
guard
let data = dataUsingEncoding(NSUTF8StringEncoding)
else { return nil }
do {
return try NSAttributedString(data: data, options: [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute:NSUTF8StringEncoding, NSFontAttributeName : UIFont.systemFontOfSize(17.0)], documentAttributes: nil)
} catch let error as NSError {
print(error.localizedDescription)
return nil
}
}
var html2String: String {
return html2AttributedString?.string ?? ""
}
}
cell.desc.attributedText = apiData[currentIndex].description!.html2AttributedString //Font attribute not recognized
cell.desc.text = apiData[currentIndex].description!.html2String //Font recognized by links no longer recognized
答案 0 :(得分:5)
我不确定为什么(可能是因为有一个范围)但是如果您首先创建一个NSMutableAttributedString然后添加UIFont属性,它就会起作用:
extension String {
var html2AttributedString: NSMutableAttributedString? {
guard
let data = dataUsingEncoding(NSUTF8StringEncoding)
else { return nil }
do {
let attrStr = try NSMutableAttributedString(data: data, options: [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute:NSUTF8StringEncoding], documentAttributes: nil)
attrStr.addAttributes([NSFontAttributeName: UIFont.systemFontOfSize(17.0)], range: NSRange(location: 0, length: attrStr.length))
return attrStr
} catch let error as NSError {
print(error.localizedDescription)
return nil
}
}
var html2String: String {
return html2AttributedString?.string ?? ""
}
}
答案 1 :(得分:1)
此处我已更新为 SWIFT 3
extension String {
var utf8Data: Data? {
return data(using: .utf8)
}
}
extension Data {
var attributedString: NSAttributedString? {
do {
return try NSAttributedString(data: self, options:[NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: String.Encoding.utf8.rawValue,NSForegroundColorAttributeName:UIColor.white,NSFontAttributeName:UIFont.systemFont(ofSize: 15)], documentAttributes: nil)
} catch let error as NSError {
print(error.localizedDescription)
}
return nil
}
var attributedStringHtml: NSMutableAttributedString? {
do {
let attrStr = try NSMutableAttributedString(data: self, options: [NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType,NSCharacterEncodingDocumentAttribute:String.Encoding.utf8.rawValue], documentAttributes: nil)
attrStr.addAttributes([NSFontAttributeName: UIFont.systemFont(ofSize: 17.0),NSForegroundColorAttributeName:UIColor.white], range: NSRange(location: 0, length: attrStr.length))
return attrStr
} catch let error as NSError {
print(error.localizedDescription)
}
return nil
}
}
<强>用法强>
DispatchQueue.global(qos: .background).async {
let attribut = self.sampleText.utf8Data?.attributedStringHtml
DispatchQueue.main.async {
self.convertedTextLbl.attributedText = attribut
}
}