无法将typesNSAttributedString.DocumentAttributeKey的值转换为.DocumentReadingOptionKey

我在互联网上的某个地方发现了这个string扩展,它允许我把html代码转换成属性string:

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抱怨:

无法将types“NSAttributedString.DocumentAttributeKey”的值转换为期望的字典键types“NSAttributedString.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的扩展应该是这样的:

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

这适用于我:

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

如果你不加

.characterEncoding:String.Encoding.utf8。 rawValue

该应用程序将崩溃。

有此后自动转换为Swift 4.通过更改来修复:

 NSMutableAttributedString(data: data, options: [NSAttributedString.DocumentAttributeKey.documentType : NSAttributedString.DocumentType.html], documentAttributes: nil) 

至:

 NSMutableAttributedString(data: data, options: [.documentType : NSAttributedString.DocumentType.html], documentAttributes: nil) { 

对于HTMLstring, NSAttributedString.DocumentType.html是正确的选项。

斯威夫特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 } } } 

我使用NSAttributedStringKey和类似的错误“无法转换types的值”在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,] 

这就是我如何将该属性添加到文本中:

 myLabel.attributedText = NSAttributedString(string: myString, attributes: TextStroke)