我在上找到了这个字符串扩展,这样我就可以将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‘类型的值转换为所需的字典键类型'NSAttributedString.DocumentReadingOptionKey’
我该如何解决这个问题?
发布于 2017-06-06 21:40:32
您需要传递一个可用的NSAttributedString DocumentType选项:
超文本标记语言文档。
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导入程序(即,选项字典包含具有HTML值的documentType )。它将尝试与主线程同步、失败和超时。从主线程调用它是可行的(但如果HTML包含对外部资源的引用,则仍然可能超时,这应该不惜一切代价避免)。HTML导入机制用于实现诸如markdown之类的东西(即文本样式、颜色等),而不是用于一般的HTML导入
发布于 2017-09-21 19:42:53
在自动转换到Swift 4后出现此问题。已通过以下更改修复:
NSMutableAttributedString(data: data,
options: [NSAttributedString.DocumentAttributeKey.documentType : NSAttributedString.DocumentType.html],
documentAttributes: nil)
至:
NSMutableAttributedString(data: data,
options: [.documentType : NSAttributedString.DocumentType.html],
documentAttributes: nil) {
发布于 2017-09-08 12:19:40
这对我来说很有效:
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
应用程序将崩溃。
https://stackoverflow.com/questions/44390520
复制相似问题