Swift 4的attributesString得到了输入属性
发布时间:2020-12-14 05:31:54 所属栏目:百科 来源:网络整理
导读:我正在尝试创建AttributedString并添加属性 typingAttributes(来自textView) 问题是 .typingAttributes 返回 [String,Any] 和 NSAttributedString(string:..,attributes:[]) 需求 [NSAttributedStringKey:Any] 我的代码: NSAttributedString(string: "te
我正在尝试创建AttributedString并添加属性
typingAttributes(来自textView) 问题是 .typingAttributes 返回 [String,Any] 和 NSAttributedString(string:..,attributes:[]) 需求 [NSAttributedStringKey:Any] 我的代码: NSAttributedString(string: "test123",attributes: self.textView.typingAttributes) 我不想创建for循环来遍历所有键并将其更改为 NSAttributedStringKey
您可以将[String:Any]字典映射到a
[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<NSAttributedStringKey,Value>(uniqueKeysWithValues: map { key,value) }) } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |