ios 如何在 UITextView 中检测用户输入字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9799215/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:26:11  来源:igfitidea点击:

How to detect user input character in UITextView

iphoneobjective-ciosuitextview

提问by Jason Zhao

How to detect user input character in UITextView?

如何在 UITextView 中检测用户输入字符?

For example:(in UITextView NOT UITextField)

例如:(在 UITextView NOT UITextField 中)

when user input letter "a", trigger an event. when user input "do it", trigger another event. or input "http://www.stackoverflow.com" trigger an event.

当用户输入字母“a”时,触发一个事件。当用户输入“do it”时,触发另一个事件。或输入“http://www.stackoverflow.com”触发事件。

Thanks

谢谢

采纳答案by Kuldeep

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 
{
    NSString * length;   
    NSString *currentString = [textField.text stringByReplacingCharactersInRange:range withString:string];
length = [currentString length];
if (length > 1)
{

    looprange = _looptext.text;
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Please ! "
                                        message:@"Insert Single Characters !!"
                                                   delegate:self
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    [alert show];
    return NO;

}
}

回答by Selvin

You can monitor the TextView content changes inside this delegate method and trigger necessary actions.

您可以监视此委托方法内的 TextView 内容更改并触发必要的操作。

- (void)textViewDidChange:(UITextView *)textView
{
    //Access the textView Content
    //From here you can get the last text entered by the user
    NSArray *stringsSeparatedBySpace = [textView.text componentsSeparatedByString:@" "];
    //then you can check whether its one of your userstrings and trigger the event
    NSString *lastString = [stringsSeparatedBySpace lastObject];
    if([lastString isEqualToString:@"http://www.stackoverflow.com"]) //add more conditions here
    {
          [self callActionMethod];
    }
}

回答by iPrabu

This delegate will be called whenever a user presses key

每当用户按下键时都会调用此委托

Try this :-

尝试这个 :-

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if([[textView.text stringByAppendingString:text] isEqualToString:@"a"])
    {
      //trigger 'a' operation
    }
    else if([[textView.text stringByAppendingString:text] isEqualToString:@"do it"])
    {
      //trigger do it operation
    }
    //Same conditions go on
}

回答by Devbot10

Working Swift 3 Solution

工作 Swift 3 解决方案

First, add UITextViewDelegate

首先,添加 UITextViewDelegate

Then, set delegate in viewDidLoadlike so, self.testTextField.delegate = self

然后,viewDidLoad像这样设置委托,self.testTextField.delegate = self

And finally, call the function textViewDidChange, example below.

最后,调用函数textViewDidChange,示例如下。

func textViewDidChange(_ textView: UITextView) {
    // Do the logic you want to happen everytime the textView changes
    // if string is == "do it" etc....

}