开发者

Want to make UITextView react to return key

I'm trying to implement a program in Xcode that's 开发者_如何学JAVAsomewhat 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?


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.
 */


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];
}


Note, I haven't tested this, just an idea:

- (void)textViewDidChange:(UITextView *)textView
{
    if ([[textView.text substringWithRange:NSMakeRange(textView.text.length - 1, 1)] isEqualToString:@"\n"])
    { 
       [textView resignFirstResponder];
       [self methodYouWantToCall];
    }
}


You can do this by setting up a delegate for the UITextView. See UITextViewDelegate Protocol Reference for details on what can be done.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜