我几天来一直在努力解决一个问题,希望你能帮助我。
在我正在编程的应用程序中,我正在使用 UITextField。
在该 TextField 中,我希望用户输入 UILabel 上看到的单词。
如果他输入正确的字符,我希望标签上的字符变成绿色,如果错误,那么标签应该变成红色。
有人可以帮助我或给我建议吗?
最佳答案
您将在 textField delegate 中获取文本,并根据您的条件调用函数 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
}
关于Swift 4 UILabel 动画颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51477447/