在 Swift 4 中设置 textView.typingAttributes 前景色



我在 Swift 3 中使用以下方法将我的 textView.typingAttributes 前景色设置为红色。

textView.typingAttributes[NSForegroundColorAttributeName] = UIColor.red

现在,我已经迁移到 Swift 4 它的显示错误,我尝试了下面,但它仍然显示错误。正确的设置方法是什么?

textView.typingAttributes[NSAttributedStringKey.foregroundColor] = UIColor.red

试试这个:

textView.typingAttributes[NSAttributedStringKey.foregroundColor.rawValue] =
UIColor.red

对于 Swift4.2 和 Swift 5.0,它是

textView.typingAttributes = [NSAttributedString.Key.foregroundColor:UIColor.red]

最新更新