无法将NSAttributedString.DocumentAttributeKey类型的值转换为.DocumentReadingOptionKey

Lin*_*rth 36 ios swift swift4 nsattributedstringkey

我在SO上找到了这个字符串扩展,允许我将html代码转换为属性字符串:

func html2AttributedString() -> NSAttributedString {
    return try! NSAttributedString(data: self.data(using: String.Encoding.unicode, allowLossyConversion: true)!, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil)
}
Run Code Online (Sandbox Code Playgroud)

它在Swift 3中运行良好,但是对于Swift 4,Xcode抱怨:

无法将'NSAttributedString.DocumentAttributeKey'类型的值转换为预期的字典键类型'NSAttributedString.DocumentReadingOptionKey'

我该如何解决?

Leo*_*bus 88

您需要传递一个可用的NSAttributedString DocumentType选项:


超文本标记语言(HTML)文档.

static let html: NSAttributedString.DocumentType
Run Code Online (Sandbox Code Playgroud)

纯文本文档.

static let plain: NSAttributedString.DocumentType
Run Code Online (Sandbox Code Playgroud)

富文本格式文档.

static let rtf: NSAttributedString.DocumentType
Run Code Online (Sandbox Code Playgroud)

带附件文档的富文本格式.

static let rtfd: NSAttributedString.DocumentType
Run Code Online (Sandbox Code Playgroud)

在这种情况下,您将需要传递第一个(html) NSAttributedString.DocumentType.html

所以更新到Swift 4 的扩展应该如下所示:

extension NSAttributedString {
    convenience init(data: Data, documentType: DocumentType, encoding: String.Encoding = .utf8) throws {
        try self.init(data: data,
                      options: [.documentType: documentType,
                                .characterEncoding: encoding.rawValue],
                      documentAttributes: nil)
    }
    convenience init(html data: Data) throws {
        try self.init(data: data, documentType: .html)
    }
    convenience init(txt data: Data) throws {
        try self.init(data: data, documentType: .plain)
    }
    convenience init(rtf data: Data) throws {
        try self.init(data: data, documentType: .rtf)
    }
    convenience init(rtfd data: Data) throws {
        try self.init(data: data, documentType: .rtfd)
    }
}
Run Code Online (Sandbox Code Playgroud)

讨论不应从后台线程调用HTML导入程序(即,选项字典包含值为html的documentType).它将尝试与主线程同步,失败和超时.从主线程调用它可以工作(但如果HTML包含对外部资源的引用,仍然可以超时,这应该不惜一切代价避免).HTML导入机制用于实现markdown(即文本样式,颜色等),而不是用于一般的HTML导入


Vit*_*lii 21

有这个后自动转换为Swift 4.通过改变来修复:

NSMutableAttributedString(data: data, 
   options: [NSAttributedString.DocumentAttributeKey.documentType : NSAttributedString.DocumentType.html], 
   documentAttributes: nil)
Run Code Online (Sandbox Code Playgroud)

至:

NSMutableAttributedString(data: data,
   options: [.documentType : NSAttributedString.DocumentType.html],
   documentAttributes: nil) {
Run Code Online (Sandbox Code Playgroud)


小智 13

这对我有用:

let attrStr = try! NSAttributedString(
    data: modifiedFont.data(using: .unicode, allowLossyConversion: true)!,
    options:[.documentType: NSAttributedString.DocumentType.html,
    .characterEncoding: String.Encoding.utf8.rawValue],
    documentAttributes: nil)
Run Code Online (Sandbox Code Playgroud)

如果你不添加

.characterEncoding:String.Encoding.utf8.rawValue

该应用程序将崩溃.


ima*_*yni 5

swift 4:我不知道为什么所有答案对我来说都有编译器错误。所以使用这个扩展名:

extension String {
    var html2AttributedString: NSAttributedString? {
        do {
            return try NSAttributedString(data: data(using: String.Encoding(rawValue: String.Encoding.utf8.rawValue))!,
                                          options: [.documentType: NSAttributedString.DocumentType.html,
                                                    .characterEncoding: String.Encoding.utf8.rawValue],
                                          documentAttributes: nil)
        } catch {
            print("error: ", error)
            return nil
        }
    }
    var html2String: String {
        return html2AttributedString?.string ?? ""
    }
}
Run Code Online (Sandbox Code Playgroud)

如何使用 ?

mylable.text = htmlVariable.html2String