【问题标题】:iOS, Swift, UITextField Changing Colour of Text being typed using typingAttributes, first letter's colour is not changediOS,Swift,UITextField 改变使用 typingAttributes 输入的文本颜色,第一个字母的颜色不会改变
【发布时间】:2018-08-09 19:15:50
【问题描述】:
我正在尝试将 UITextfield 中输入的文本颜色更改为白色。我使用了以下代码,该代码有效,但不会将第一个字母的颜色更改为白色。请参阅随附的屏幕截图。
如何让它工作,使第一个字母的颜色也变为白色?
我搜索了很多,但找不到解决方案。
提前发送!
@IBAction func emailFieldEditingChanged(_ sender: UITextField) {
emailTextField.typingAttributes![NSAttributedStringKey.foregroundColor.rawValue] = UIColor.white
}
【问题讨论】:
标签:
ios
swift
uitextfield
swift4
【解决方案1】:
您需要将代理设置为 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