How to detect user input character in UITextView
33,120
Solution 1
- (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;
}
}
Solution 2
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];
}
}
Solution 3
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
}
Solution 4
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....
}
Author by
Jason Zhao
Updated on September 05, 2020Comments
-
Jason Zhao over 3 years
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
-
Leena about 12 yearswhy don't you try UITextFieldDelegate methods for that.
-
Narayana about 12 years- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text it will fire for every charter press in textView
-
Jason Zhao about 12 years@Narayana you are the first correct. Next time you can put the answer to "Answer field". Then I can tick you~ Thanks!
-
-
Kuldeep about 12 yearsThats okay!! It was just a pattern, it would be changed as per your purpose of usage.
-
Mark Amery almost 11 years-1; the question explicitly asked for detecting content changes on a
UITextView
and not aUITextField
. This is wrong and I don't know why the question asker accepted it. The analagous method inUITextViewDelegate
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 stackoverflow.com/questions/7010547/…). -
Selvin about 10 years-1 Agreeing with @MarkAmery about the irrelevancy of the answer here.
-
SinisterMJ over 8 yearsYou cannot just append text, you have to take into account the range of where the changes occurred.
-
GenericJam about 6 yearsUITextFieldDelegate should be UITextViewDelegate
-
Duan Nguyen about 6 yearsThis function is more useful than textViewDidChange function, especially in case user uses the suggestion, instead of entering with keyboard.