无法转换类型 'NSAttributedString.DocumentAttributeKey' 的值,"DocumentReadingOptionKey" 也不起作用
Cannot convert value of type 'NSAttributedString.DocumentAttributeKey' , "DocumentReadingOptionKey" also not working
我从 Swift 3 升级到 Swift 4,我收到 NSAttributedString
相关错误。
这是我的代码:
viewModel.getInviteAFriendInfo(success: { message in
if message.isEmpty{
self.lblDetail.text = "xxxxxxx"
}else{
let htmlString: String = "<html><head><style xxxxxx </style></head><body>\(message)</body></html>"
self.lblDetail.setText(try! NSAttributedString(data: htmlString.data(using: .unicode, allowLossyConversion: true)!, options: [NSAttributedString.DocumentAttributeKey.documentType:NSAttributedString.DocumentType.html,NSAttributedStringKey.kern: 2.0], documentAttributes: nil))
}
}, failure: {
self.showAlert("Failed to get text")
})
这是我的错误:
Cannot convert value of type 'NSAttributedString.DocumentAttributeKey' to expected dictionary key type 'NSAttributedString.DocumentReadingOptionKey'
阅读了一些已发布的问题和解决方案,尝试将 NSAttributedString.DocumentAttributedKey
更改为 NSAttributedString.DocumentReadingOptionKey
,但仍然出错。
您尝试传递给 options
参数的值需要拆分。
首先,documentType
来自 NSAttributedString.DocumentReadingOptionKey
,而不是 NSAttributedString.DocumentAttributeKey
。
其次,kern
需要传递给 documentAttributes
参数,而不是 options
参数。
如果你拆分代码就容易多了:
let options: [NSAttributedString.DocumentReadingOptionKey : Any] = [ .documentType: NSAttributedString.DocumentType.html ]
let attributes = [ NSAttributedString.Key.kern: 2.0 ] as NSDictionary?
let data = htmlString.data(using: .utf8)!
let attrStr = try? NSAttributedString(data: data, options: options, documentAttributes: &attributes)
试试这个
var htmlString: String = "<html><head><style xxxxxx </style></head><body>\(message)</body></html>"
if let attributedString = try? NSAttributedString(data: (htmlString.data(using: .unicode, allowLossyConversion: true)!), options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil) {
self.lblDetail.attributedText = attributedString
}
我从 Swift 3 升级到 Swift 4,我收到 NSAttributedString
相关错误。
这是我的代码:
viewModel.getInviteAFriendInfo(success: { message in
if message.isEmpty{
self.lblDetail.text = "xxxxxxx"
}else{
let htmlString: String = "<html><head><style xxxxxx </style></head><body>\(message)</body></html>"
self.lblDetail.setText(try! NSAttributedString(data: htmlString.data(using: .unicode, allowLossyConversion: true)!, options: [NSAttributedString.DocumentAttributeKey.documentType:NSAttributedString.DocumentType.html,NSAttributedStringKey.kern: 2.0], documentAttributes: nil))
}
}, failure: {
self.showAlert("Failed to get text")
})
这是我的错误:
Cannot convert value of type 'NSAttributedString.DocumentAttributeKey' to expected dictionary key type 'NSAttributedString.DocumentReadingOptionKey'
阅读了一些已发布的问题和解决方案,尝试将 NSAttributedString.DocumentAttributedKey
更改为 NSAttributedString.DocumentReadingOptionKey
,但仍然出错。
您尝试传递给 options
参数的值需要拆分。
首先,documentType
来自 NSAttributedString.DocumentReadingOptionKey
,而不是 NSAttributedString.DocumentAttributeKey
。
其次,kern
需要传递给 documentAttributes
参数,而不是 options
参数。
如果你拆分代码就容易多了:
let options: [NSAttributedString.DocumentReadingOptionKey : Any] = [ .documentType: NSAttributedString.DocumentType.html ]
let attributes = [ NSAttributedString.Key.kern: 2.0 ] as NSDictionary?
let data = htmlString.data(using: .utf8)!
let attrStr = try? NSAttributedString(data: data, options: options, documentAttributes: &attributes)
试试这个
var htmlString: String = "<html><head><style xxxxxx </style></head><body>\(message)</body></html>"
if let attributedString = try? NSAttributedString(data: (htmlString.data(using: .unicode, allowLossyConversion: true)!), options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil) {
self.lblDetail.attributedText = attributedString
}