NSFontAttributeName 未应用于 NSAttributedString

NSFontAttributeName not applied to NSAttributedString

我目前正在使用此 提供的扩展将 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

我不确定为什么(可能是因为有一个范围)但是如果你先创建一个 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
            }
        }