15

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

Jason Zhao
  • 1,278
  • 4
  • 19
  • 36

4 Answers4

27

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];
    }
}
Selvin
  • 12,333
  • 17
  • 59
  • 80
11

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
}
Leena
  • 2,678
  • 1
  • 30
  • 43
ipraba
  • 16,485
  • 4
  • 59
  • 58
8

Working Swift 3 Solution

First, add UITextViewDelegate

Then, set delegate in viewDidLoad like so, self.testTextField.delegate = self

And finally, call the function textViewDidChange, example below.

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

}
Juri Noga
  • 4,363
  • 7
  • 38
  • 51
Devbot10
  • 1,193
  • 18
  • 33
-14
- (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;

}
}
Ramani Hitesh
  • 214
  • 3
  • 15
Kuldeep
  • 2,589
  • 1
  • 18
  • 28
  • 1
    Thats okay!! It was just a pattern, it would be changed as per your purpose of usage. – Kuldeep Mar 22 '12 at 05:57
  • 8
    -1; the question explicitly asked for detecting content changes on a `UITextView` and *not* a `UITextField`. This is wrong and I don't know why the question asker accepted it. The analagous method in `UITextViewDelegate` is, as mentioned in other answers, `- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text` (and both may be inappropriate for the asker's use case; see http://stackoverflow.com/questions/7010547/uitextfield-text-change-event). – Mark Amery Jul 25 '13 at 11:53
  • 2
    -1 Agreeing with @MarkAmery about the irrelevancy of the answer here. – Selvin Apr 09 '14 at 03:11