首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >无法将NSAttributedString.DocumentAttributeKey类型的值转换为.DocumentReadingOptionKey

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

Stack Overflow用户
提问于 2017-06-06 20:51:02
回答 8查看 31.7K关注 0票数 39

我在上找到了这个字符串扩展,这样我就可以将html代码转换为属性字符串:

代码语言:javascript
复制
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‘类型的值转换为所需的字典键类型'NSAttributedString.DocumentReadingOptionKey’

我该如何解决这个问题?

EN

回答 8

Stack Overflow用户

回答已采纳

发布于 2017-06-06 21:40:32

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

超文本标记语言文档。

代码语言:javascript
复制
static let html: NSAttributedString.DocumentType

纯文本文档。

代码语言:javascript
复制
static let plain: NSAttributedString.DocumentType

富文本格式文档。

代码语言:javascript
复制
static let rtf: NSAttributedString.DocumentType

带有附件文档的

富文本格式。

代码语言:javascript
复制
static let rtfd: NSAttributedString.DocumentType

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

因此,Swift 4的扩展updated应该如下所示:

代码语言:javascript
复制
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)
    }
}

代码语言:javascript
复制
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
    }
}

代码语言:javascript
复制
extension Data {
    var htmlToAttributedString: NSAttributedString? {
        do {
            return try .init(html: self)
        } catch {
            print("html error:", error)
            return nil
        }

    }
}

游乐场测试

代码语言:javascript
复制
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导入程序(即,选项字典包含具有HTML值的documentType )。它将尝试与主线程同步、失败和超时。从主线程调用它是可行的(但如果HTML包含对外部资源的引用,则仍然可能超时,这应该不惜一切代价避免)。HTML导入机制用于实现诸如markdown之类的东西(即文本样式、颜色等),而不是用于一般的HTML导入

票数 92
EN

Stack Overflow用户

发布于 2017-09-21 19:42:53

在自动转换到Swift 4后出现此问题。已通过以下更改修复:

代码语言:javascript
复制
NSMutableAttributedString(data: data, 
   options: [NSAttributedString.DocumentAttributeKey.documentType : NSAttributedString.DocumentType.html], 
   documentAttributes: nil)

至:

代码语言:javascript
复制
NSMutableAttributedString(data: data,
   options: [.documentType : NSAttributedString.DocumentType.html],
   documentAttributes: nil) {
票数 23
EN

Stack Overflow用户

发布于 2017-09-08 12:19:40

这对我来说很有效:

代码语言:javascript
复制
let attrStr = try! NSAttributedString(
    data: modifiedFont.data(using: .unicode, allowLossyConversion: true)!,
    options:[.documentType: NSAttributedString.DocumentType.html,
    .characterEncoding: String.Encoding.utf8.rawValue],
    documentAttributes: nil)

如果你不添加

String.Encoding.utf8.rawValue

.characterEncoding

应用程序将崩溃。

票数 14
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44390520

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档