Want to make UITextView react to return key

回眸只為那壹抹淺笑 提交于 2021-02-08 15:51:30

问题


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?


回答1:


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



回答2:


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



回答3:


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



回答4:


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



来源:https://stackoverflow.com/questions/7716104/want-to-make-uitextview-react-to-return-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!