Starten durch die Abbildung des Textes jeder Farbe seiner entsprechenden UIColor
:
let colors: [String: UIColor] = [
// add any built-in colors
"red": .red,
"blue": .blue,
// custom colors too!
"goldenrod": UIColor(red: 218, green: 165, blue: 32)
// add all the colors you wish to detect
// ...
// ...
]
Machen Sie den View-Controller Ihr Textfeld UITextViewDelegate
konform enthält und implementieren die shouldChangeTextIn
Funktion (wird aufgerufen, wenn der Benutzer eingibt/löscht ein Zeichen):
class MyViewController: UIViewController, UITextFieldDelegate {
// ... other view controller code
func textField(_ textField: UITextField, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
if let currentText = textField.text {
// check if the text is in the `colors` dictionary
// and if so, apply the color to the `textColor`
if colors.keys.contains(currentText) {
textField.textColor = colors[currentText]
}
}
return true
}
// ... other view controller code
}
Haben Sie etwas versucht, so weit? Wie sieht dein Code aus? – ZGski
@ZGski Ich habe nur ein Grundlayout für das Textfeld erstellt und eine Steckdose angeschlossen. Verwirrt, wie man sich ihm nähert. – iVvaibhav
Ehrlich, ich denke, es würde allen nützen, wenn du es vielleicht selbst ausprobieren und die Dinge selbst testen würdest. Stack Overflow ist eher zum Debuggen gedacht oder wenn Sie absolut ratlos sind - nicht um Dinge wie "Wie soll ich das Ding machen?" Anzufordern. – ZGski