简体   繁体   English

Swift 代码将在键入时打印 UITextView 中键入的新单词的所有字符的字符串

[英]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.我需要有关将在 UITextView 函数 textViewDidChange 的主体中运行的 swift 代码或函数的帮助,该函数将打印一个字符串,其中包含在 UITextView 控件中键入的新 Word 的所有字母。 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.因此,如果我的用户界面上有一个名为 txtTextView 的 UITextView 控件,并且当应用程序开始运行时它没有文本,如果我输入字母“o”,那么字母“o”将打印在控制台输出屏幕中。 After I have typed "or" then "or" will be printed.输入“或”后,将打印“或”。 After I have typed "orange" then "orange" will be printed.输入“orange”后,将打印“orange”。 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.如果在输入橙色后我按空格键并开始输入“ma”然后将打印“ma”,在我输入“orange mango”后将打印“mango”。 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.基本上我希望代码打印每个新单词的所有字母,因为它们在 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.只需为您的textField创建一个插座,添加UITextViewDelegate并设置委托。 After that just override the textViewDidChange method.之后只需覆盖textViewDidChange方法。 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!)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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