我在某个地方找到了一个字符串,可以将其转换为html代码:

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

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

无法转换"NSAttributedString"类型的值.DocumentAttributeKey"应为字典键类型"NSAttributeString.DocumentReadingOptionKey'

我该怎么解决这个问题?

推荐答案

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


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

static let html: NSAttributedString.DocumentType

纯文本文档.

static let plain: NSAttributedString.DocumentType

富文本格式文档.

static let rtf: NSAttributedString.DocumentType

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

static let rtfd: NSAttributedString.DocumentType

在这种情况下,您需要通过第一个(html)NSAttributedString.DocumentType.html

所以Swift 4的分机updated应该是这样的:

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)
    }
}

extension StringProtocol {
    var data: Data { return Data(utf8) }
    var htmlToAttributedString: NSAttributedString? {
        do {
            return try .init(html: data)
        } catch {
            print("html error:", error)
            return nil
        }
    }
    var htmlDataToString: String? {
        return htmlToAttributedString?.string
    }
}

extension Data {
    var htmlToAttributedString: NSAttributedString? {
        do {
            return try .init(html: self)
        } catch {
            print("html error:", error)
            return nil
        }

    }
}

操场测试

let htmlString = "<style type=\"text/css\">#red{color:#F00}#green{color:#0F0}#blue{color: #00F; font-weight: Bold; font-size: 32}</style><span id=\"red\" >Red</span><span id=\"green\" > Green </span><span id=\"blue\">Blue</span>"

let htmlData = Data(htmlString.utf8)

htmlString.htmlToAttributedString
htmlData.htmlToAttributedString

讨论不应在后台调用HTML导入程序

Swift相关问答推荐

在SWIFTUI&39;S视图修改器中使用等待关键字

如何将多个完成处理程序转换为异步?

SwiftData:非法try 在不同上下文中的对象之间建立关系

无论玩家移动到视图内的哪个位置,如何使用 SpriteKit 让敌人向玩家emits 子弹?

仅当 boolean 为真时才实现方法(application:didReceiveRemoteNotification)

URL appendPathComponent(_:) 已弃用?

deinitialize() 与 deallocate()

为什么 String.contains 在我导入 Foundation 时表现不同?

如何打印出此 struct 中的数据?

并发执行代码中捕获的 var 的变异

如何避免从模块导入函数

如何在 SwiftUI 中将图像传递到 2 个视图

从 Swift 列表中的行中检索值

SwiftUI Preview 不适用于 Core Data

SwiftUI 文本被意外截断

在 SwiftUI 中,如何在 UIView 内或作为 UIView 使用 UIHostingController?

让我的函数计算数组 Swift 的平均值

无法分配给属性:self 是不可变的,我知道如何修复但需要理解

Swift - 迭代 struct 对象时如何对其进行变异

通过单击表格视图中的单元格打开新的视图控制器 - Swift iOS