Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect user input character in UITextView

How to detect user input character in UITextView?

For example:(in 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.

Thanks

like image 887
Jason Zhao Avatar asked Mar 21 '12 05:03

Jason Zhao


1 Answers

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

- (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];
    }
}
like image 124
Selvin Avatar answered Sep 20 '22 21:09

Selvin