我这几天一直在努力解决一个问题,希望你能帮助我。
在我正在编程的应用程序中,我正在使用 UITextField。在该 TextField 中,我希望用户键入在 UILabel 上看到的单词。
如果他输入正确的字符,我希望标签上的字符变为绿色,如果输入错误,则标签应变为红色。
任何人都可以帮助我或给我建议吗?
您将在textField 委托中获取文本,并根据您的条件调用函数updateLabel ()
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
self.updateLabel(true, text: string)
return true
}
func updateLabel(isCorrect:Bool, text: String){
let textColor = isCorrect ? greenColor : redColor
let attributedStringColor = [NSAttributedStringKey.foregroundColor : textColor];
let attributedString = NSAttributedString(string: text, attributes: attributedStringColor)
label.attributedText = attributedString
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句