Ios NSFontAttributeName未应用于NSAttributeString

Ios NSFontAttributeName未应用于NSAttributeString,ios,swift,nsattributedstring,Ios,Swift,Nsattributedstring,我目前正在使用它提供的扩展将html转换为UITextView中的字符串。一切工作都很完美,但由于某种奇怪的原因,NSFontAttributeName在处理过程中没有应用于字符串。我有什么地方做错了吗?(或者我应该在重新定义属性化html解析字符串后应用NSAttributeString?如果是,是否可以将属性应用于NSAttributeString) 注:使用html2String时,字体大小不会改变,但是UITextView会识别html中的链接 extension String {

我目前正在使用它提供的扩展将html转换为
UITextView
中的字符串。一切工作都很完美,但由于某种奇怪的原因,
NSFontAttributeName
在处理过程中没有应用于字符串。我有什么地方做错了吗?(或者我应该在重新定义属性化html解析字符串后应用
NSAttributeString
?如果是,是否可以将属性应用于
NSAttributeString

注:使用
html2String
时,字体大小不会改变,但是
UITextView
会识别html中的链接

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
我不知道为什么(可能是因为有一个范围),但如果您首先创建一个NSMutableAttributeString,然后添加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
            }
        }

这是正常的行为
NSFontAttributeName
不是
initWithData:options:documentsAttributes:error:
中的
options
参数所允许的键。之后你必须重写字体效果。因此,要么通过将HTML添加到原始字符串中来应用它,要么之后(这样可能会删除粗体/斜体效果)。可能重复@Larme谢谢您提供这些详细信息,我不知道此方法中忽略了此属性。如果他们决定这样转换,我将把我的答案留给OP。一点警告:如果HTML部分中有粗体或斜体效果,那么将字体设置为这样将删除它(因为它位于
UIFont
/
NSFontAttributeName
部分)。它不起作用的原因是,
NSFontAttributeName
在该init方法中不是有效的键(并且被忽略)。非常有效,谢谢Eric@Larme我不会设置任何粗体或斜体效果,这个答案适合我的需要。谢谢你的邀请information@kye不客气。:)再次感谢Larme提供的额外信息。@EricAya工作得很好。谢谢你的回答!