作者:轻梦云裳_904 | 来源:互联网 | 2023-01-16 12:13
1> Martin R..:
您可以将[String: Any]
字典映射到
[NSAttributedStringKey: Any]
字典
let typingAttributes = Dictionary(uniqueKeysWithValues: self.textView.typingAttributes.map {
key, value in (NSAttributedStringKey(key), value)
})
let text = NSAttributedString(string: "test123", attributes: typingAttributes)
这是一个可能的扩展方法,它仅限于使用字符串键的字典:
extension Dictionary where Key == String {
func toAttributedStringKeys() -> [NSAttributedStringKey: Value] {
return Dictionary(uniqueKeysWithValues: map {
key, value in (NSAttributedStringKey(key), value)
})
}
}