2017-07-28 4 views
-1

Ich habe eine UITextView und ich muss die String erkennen, die eine Farbe sein wird, die der Benutzer darin eingegeben hat. Dann muss ich die Farbe des Textes in der UITextField zu der Farbe ändern, die vom Benutzer eingegeben wird.Ändern Sie die Farbe des Textes UITextField zu der eingegebenen Farbe

Zum Beispiel: wenn ich 'rot' in textField eintippe, sollte die Farbe des Textes zu rot wechseln.

Weiß jemand, wie das geht?

Danke!

+1

Haben Sie etwas versucht, so weit? Wie sieht dein Code aus? – ZGski

+0

@ZGski Ich habe nur ein Grundlayout für das Textfeld erstellt und eine Steckdose angeschlossen. Verwirrt, wie man sich ihm nähert. – iVvaibhav

+1

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

Antwort

0

Als Ausgangspunkt müssen Sie eine Zuordnung von String zu UIColor erstellen.

let colorMapping: [String: UIColor] = [ 
    "green": .green, 
    "white": .white, 
    //etc... 
] 
0

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 

} 
0

Sie können den Text in Textfeld mit der folgenden Delegate-Methode erkennen

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool 
{ 
     //Don't forgot to make user entered content to lowercaseString while checking with colorCollection 

     if colorCollection.keys.contains(textField.text.lowercaseString) { 
      textField.textColor = colorCollection[currentText] //If entered content match with any colour then it will change the text colour of textfield 
     } 

    return true 
} 

Erstellen Sie einfache Sammlung von Farbe wie unter

let colorCollection: [String: UIColor] = [ 
    "blue": .blue, 
    "purple": .purple, 
    //your other coolers 
] 
0

So vorausgesetzt, Sie haben sehene Feld ein und Textview Steckdose oder programmatisch erstellt: -

let textField = UITextField() 
let txtVw = UITextView() 
//If enter some text inside textfield for instance red then get the value, compare it and set the text color on textview 
if textField.text?.lowercased() == "red" { 
txtVw.textColor = UIColor.red 
} 
Verwandte Themen