iOS、Swift、UITextField 使用 typingAttributes 更改正在键入的文本的颜色,第一个字母的颜色未更改

iOS, Swift, UITextField Changing Colour of Text being typed using typingAttributes, first letter's colour is not changed

我正在尝试将 UITextfield 中键入的文本颜色更改为白色。我使用了以下代码,该代码有效但不会将第一个字母的颜色更改为白色。请参阅随附的屏幕截图。

如何使第一个字母的颜色也变为白色?

我搜索了很多但找不到解决方案。

提前Tx!

@IBAction func emailFieldEditingChanged(_ sender: UITextField) {

    emailTextField.typingAttributes![NSAttributedStringKey.foregroundColor.rawValue] = UIColor.white
}

您可能希望像 Rakesha 提到的那样设置委托,但每次用户输入字符时都必须更改它。使用此委托方法。

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    emailTextField.typingAttributes![NSAttributedStringKey.foregroundColor.rawValue] = UIColor.white
    return true
}

//Set this in viewDidLoad, and don't forget to include the delegate.
emailTextField.delegate = self