Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Want to make UITextView react to return key

I'm trying to implement a program in Xcode that's somewhat like a command line. I basically have a UITextView that can take multiple lines of text. Right now, I have a button that will make the necessary changes after the user is done entering commands, but I want to be able to have a method that gets called after the user hits the return key in the UITextView, so basically it makes changes after each command. Is it possible to do this?

like image 438
RaysonK Avatar asked Oct 10 '11 16:10

RaysonK


2 Answers

The BOOL method mentioned above is a wrong answer... for one the person is checking the text from the TextView the moment before it is updated so they are viewing the old text... Also the methods are out of date. This usage will work immediately once the return key is pressed (The current "answer" will not work until after the return key has been pressed and then ANOTHER key is pressed):

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if ([text isEqualToString:@"\n"]) {
        NSLog(@"Return pressed");
    } else {
        NSLog(@"Other pressed");
    }
    return YES;
}

Don't forget to add the UITextViewDelegate to your .h file's protocols.

@interface ViewController : UIViewController <UITextViewDelegate> {

and set yourTextView.delegate = self; in .m file!


/* 
note: This will also get called if a user copy-pastes just a line-break... 
 unlikely but possible. If you need to ignore pasted line-breaks for some 
 reason see here: http://stackoverflow.com/a/15933860/2057171 ... 
 Now for an unrelated-tip: If you want to accept pasted line breaks however 
 I suggest you add an "or" to the conditional statement and make it also 
 check if "text" isEqualToString @"\r" which is another form of line-break 
 not found on the iOS keyboard but it can, however, be found on a website 
 and copy-pasted into your textView.  If you want to accept pasted text 
 with a line-break at the end you will need to change the 
 "isEqualToString" code above to say "hasSuffix", this will check for 
 any string %@ with a "\n" at the end. (and again for "\r") but make 
 sure you don't call your "next" method until after `return YES;` 
 has been called and the text view has been updated, otherwise 
 you will get only the text that was there before the copy paste 
 since this is "shouldChangeTextInRange" method, not 
 "didChangeTextInRange", if you do this I suggest stripping the 
 "\n" or "\r" from the end of your final string after the copy-paste 
 was made and applied and the text was updated.
 */
like image 169
Albert Renshaw Avatar answered Sep 30 '22 14:09

Albert Renshaw


If you set a delegate to the UITextView which implements

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text

Then you can check if the last character is "\n", and get the text entered since the last command by doing

NSArray* components = [textView.text componentsSeparatedByString:@"\n"];
if ([components count] > 0) {
    NSString* commandText = [components lastObject];
    // and optionally clear the text view and hide the keyboard...
    textView.text = @"";
    [textView resignFirstResponder];
}
like image 37
jbat100 Avatar answered Sep 30 '22 16:09

jbat100