NSFontAttributeName不应用于NSAttributedString

我目前正在使用这个答案提供的这个扩展转换为UITextViewstring。 一切正常,但由于一些奇怪的原因NSFontAttributeName不适用于string的过程中。 有什么我在这里做错了吗? (或者我应该在应用NSAttributedString之后赋予属性的HTMLparsing的string?如果是这样,是否有可能将一个属性应用于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 

我不知道为什么 (可能是因为有一个范围),但它的工作原理,如果你先创build一个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 ?? "" } } 

在这里我已经更新到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 } }