ios 无法将 NSAttributedString.DocumentAttributeKey 类型的值转换为 .DocumentReadingOptionKey
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44390520/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot convert value of type NSAttributedString.DocumentAttributeKey to .DocumentReadingOptionKey
提问by LinusGeffarth
I found this string extension somewhere on SO that allows me to turn html code into an attributed string:
我在 SO 某处找到了这个字符串扩展名,它允许我将 html 代码转换为属性字符串:
func html2AttributedString() -> NSAttributedString {
return try! NSAttributedString(data: self.data(using: String.Encoding.unicode, allowLossyConversion: true)!, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil)
}
It worked fine in Swift 3, but with Swift 4, Xcode complains:
它在 Swift 3 中运行良好,但在 Swift 4 中,Xcode 抱怨:
Cannot convert value of type 'NSAttributedString.DocumentAttributeKey' to expected dictionary key type 'NSAttributedString.DocumentReadingOptionKey'
无法将“NSAttributedString.DocumentAttributeKey”类型的值转换为预期的字典键类型“NSAttributedString.DocumentReadingOptionKey”
How do I fix this?
我该如何解决?
回答by Leo Dabus
You need to pass one of the available NSAttributedString DocumentTypeoptions:
您需要传递可用的 NSAttributedString DocumentType选项之一:
Hypertext Markup Language (HTML) document.
超文本标记语言 (HTML) 文档。
static let html: NSAttributedString.DocumentType
Plain text document.
纯文本文档。
static let plain: NSAttributedString.DocumentType
Rich text format document.
富文本格式文档。
static let rtf: NSAttributedString.DocumentType
Rich text format with attachments document.
带有附件文档的富文本格式。
static let rtfd: NSAttributedString.DocumentType
In this case you will need to pass the first one (html) NSAttributedString.DocumentType.html
在这种情况下,您需要传递第一个 (html) NSAttributedString.DocumentType.html
So the extension updatedto Swift 4 should look like this:
所以更新到 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)
}
}
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
}
}
}
Playground Testing
游乐场测试
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
Discussion The HTML importer should not be called from a background thread (that is, the options dictionary includes documentType with a value of html). It will try to synchronize with the main thread, fail, and time out. Calling it from the main thread works (but can still time out if the HTML contains references to external resources, which should be avoided at all costs). The HTML import mechanism is meant for implementing something like markdown (that is, text styles, colors, and so on), not for general HTML import
讨论 不应从后台线程调用 HTML 导入程序(即,选项字典包含值为 html 的 documentType)。它将尝试与主线程同步,失败并超时。从主线程调用它是有效的(但如果 HTML 包含对外部资源的引用,则仍然会超时,应该不惜一切代价避免这种情况)。HTML 导入机制是为了实现诸如 markdown 之类的东西(即文本样式、颜色等),而不是用于一般的 HTML 导入
回答by Vitalii
Had this after automatic conversion to Swift 4. Was fixed by changing from:
自动转换为 Swift 4 后出现此问题。通过更改以下内容进行修复:
NSMutableAttributedString(data: data,
options: [NSAttributedString.DocumentAttributeKey.documentType : NSAttributedString.DocumentType.html],
documentAttributes: nil)
to:
到:
NSMutableAttributedString(data: data,
options: [.documentType : NSAttributedString.DocumentType.html],
documentAttributes: nil) {
回答by hall.keskin
This works for me:
这对我有用:
let attrStr = try! NSAttributedString(
data: modifiedFont.data(using: .unicode, allowLossyConversion: true)!,
options:[.documentType: NSAttributedString.DocumentType.html,
.characterEncoding: String.Encoding.utf8.rawValue],
documentAttributes: nil)
If you don't add
如果你不添加
.characterEncoding: String.Encoding.utf8.rawValue
.characterEncoding:String.Encoding.utf8。原始值
the app will crash.
该应用程序将崩溃。
回答by iman kazemayni
swift 4: I dont know why all the answers have compiler error for me. so use this extension:
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 ?? ""
}
}
how to use ?
如何使用 ?
mylable.text = htmlVariable.html2String
mylable.text = htmlVariable.html2String
回答by Suhit Patil
For HTML string, NSAttributedString.DocumentType.html
is the correct option.
对于 HTML 字符串,NSAttributedString.DocumentType.html
是正确的选项。
Swift 4
斯威夫特 4
extension String {
var utfData: Data? {
return self.data(using: .utf8)
}
var htmlAttributedString: NSAttributedString? {
guard let data = self.utfData else {
return nil
}
do {
return try NSAttributedString(data: data,
options: [
NSAttributedString.documentType: NSAttributedString.DocumentType.html,
NSAttributedString.characterEncoding: String.Encoding.utf8.rawValue
], documentAttributes: nil)
} catch {
print(error.localizedDescription)
return nil
}
}
}
回答by Bhanu
Use NSAttributedString.DocumentType.html
用 NSAttributedString.DocumentType.html
NSMutableAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html] , documentAttributes: nil)
回答by tomDev
I use NSAttributedStringKey and had similar error "Cannot convert value of type" on Swift 4. In case anyone using NSAttributedStringKey comes here looking for an answer, this is how I fixed:
我使用 NSAttributedStringKey 并且在 Swift 4 上有类似的错误“无法转换类型的值”。如果有人使用 NSAttributedStringKey 来这里寻找答案,我就是这样解决的:
let TextStroke: [NSAttributedStringKey : Any] = [
NSAttributedStringKey(rawValue: NSAttributedStringKey.strokeColor.rawValue) : UIColor.black,
NSAttributedStringKey(rawValue: NSAttributedStringKey.foregroundColor.rawValue) : UIColor.white,
NSAttributedStringKey(rawValue: NSAttributedStringKey.strokeWidth.rawValue) : -6.0,]
And this is how I add the attribute to the text:
这就是我将属性添加到文本的方式:
myLabel.attributedText = NSAttributedString(string: myString, attributes: TextStroke)