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
  • 18
  • 36
  • why don't you try UITextFieldDelegate methods for that. – Leena Mar 21 '12 at 05:48
  • 3
    - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text it will fire for every charter press in textView – Narayana Mar 21 '12 at 05:49
  • @Narayana you are the first correct. Next time you can put the answer to "Answer field". Then I can tick you~ Thanks! – Jason Zhao Mar 22 '12 at 05:53

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,013
  • 16
  • 58
  • 79
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,668
  • 1
  • 29
  • 43
ipraba
  • 16,335
  • 3
  • 56
  • 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,313
  • 7
  • 35
  • 49
Devbot10
  • 1,163
  • 16
  • 29
-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
  • 204
  • 3
  • 15
Kuldeep
  • 2,599
  • 1
  • 17
  • 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