简体   繁体   中英

Swift code that will print a string of all the characters of a new word typed in a UITextView as they are being typed

I need help with swift code or function that will run in the body of UITextView function textViewDidChange that will print a string of all the letters of a new Word as they are being typed in a UITextView control. So if I have a UITextView control on my user interface named txtTextView and it has no text in it when the application starts running if I then type letter "o" then letter "o" will be printed in the console out put screen. After I have typed "or" then "or" will be printed. After I have typed "orange" then "orange" will be printed. If after typing orange I press space bar and start typing "ma" then "ma" will be printed and after I have typed "orange mango" then "mango" will be printed. Hope I have conveyed the idea. Basically I would like the code to print all the letters of every new word as they are being typed in the UITextView. Many thanks for your help.

func textViewDidChange( textView: UITextView ) {
 // code for this place
}

Just create an outlet to your textField , add the UITextViewDelegate and set the delegate. After that just override the textViewDidChange method. Code example below

@IBOutlet weak var textView: UITextView!

override func viewDidLoad() {
    super.viewDidLoad()

    textView.delegate = self
}

@IBAction func textField_EditingChanged(sender: UITextField) {
    // Will print out each letter that is typed
    print(sender.text!.characters.last!)

    // Will print out the word
    print(sender.text!)
}

func textViewDidChange(textView: UITextView) {
    // Will print out each letter that is typed
    print(textView.text!.characters.last!)

    // Will print out the word
    print(textView.text!)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM