UItextview使用NSAttributedString

时间:2018-02-08 15:11:34

标签: swift uitextview nsattributedstring uicolor

我有一个Uitextview

我在文字字符串颜色紫色

中有一个这样的代码
 let titleAttributes = [NSAttributedStringKey.font: UIFont.preferredFont(forTextStyle: UIFontTextStyle.headline), NSAttributedStringKey.foregroundColor: UIColor.purple]

            let titleString = NSAttributedString(string: "Hello World", attributes: titleAttributes)
            txtEditor.attributedText = titleString

在此代码之后我想用uitextview(实时)写入黑色 例如在这个函数中我需要写

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {

return true
    }

如何在此textView

的继续中以其他颜色书写

1 个答案:

答案 0 :(得分:1)

要配置用户键入的颜色,请设置文本视图的输入属性。

https://developer.apple.com/documentation/uikit/uitextview/1618629-typingattributes