繁体   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

我需要有关将在 UITextView 函数 textViewDidChange 的主体中运行的 swift 代码或函数的帮助,该函数将打印一个字符串,其中包含在 UITextView 控件中键入的新 Word 的所有字母。 因此,如果我的用户界面上有一个名为 txtTextView 的 UITextView 控件,并且当应用程序开始运行时它没有文本,如果我输入字母“o”,那么字母“o”将打印在控制台输出屏幕中。 输入“或”后,将打印“或”。 输入“orange”后,将打印“orange”。 如果在输入橙色后我按空格键并开始输入“ma”然后将打印“ma”,在我输入“orange mango”后将打印“mango”。 希望我已经传达了这个想法。 基本上我希望代码打印每个新单词的所有字母,因为它们在 UITextView 中被输入。 非常感谢您的帮助。

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

只需为您的textField创建一个插座,添加UITextViewDelegate并设置委托。 之后只需覆盖textViewDidChange方法。 下面的代码示例

@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