How can I turn off the keyboard of UITextView with the return key?

In IB's library, the introduction tells us that when you press the back key, UITextView's keyboard disappears. In practice, however, the return key can only act as' \ \ n '.

I can add a button and hide the keyboard using [txtview designfirstresponder].

But is there a way to add actions to the keyboard for the return key so I don't need to add uibuttons?

#1 building

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

yourtextView.delegate=self;

Also add UITextViewDelegate

Don't forget to confirm the agreement

If you do not add an if([text isEqualToString:@"\n"]), you cannot edit it

#2 building

Add observers to viewDidLoad

[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(textViewKeyPressed:) name: UITextViewTextDidChangeNotification object: nil];

Then use the selector to check "\ \ n"

-(void) textViewKeyPressed: (NSNotification*) notification {

  if ([[[notification object] text] hasSuffix:@"\n"])
  {
    [[notification object] resignFirstResponder];
  }
}

It does use "\ \ n" instead of checking the return key specifically, but I think that's OK.

To update

Refer to the following ribto answer, which uses [NSCharacterSet newlineCharacterSet] instead of \ \ n

#3 building

A more elegant way is to turn it off when the user clicks somewhere outside the keyboard frame.

First, set the view of ViewController to the UIControl class in the identity checker of UIBuilder. Hold down the Control key and drag the view to the header file of ViewController, and link it as an operation and event (such as Touch Up Inside), for example:

ViewController.h

-(IBAction)dismissKeyboardOnTap:(id)sender;

In the main ViewController file, ViewController.m:

-(IBAction)dismissKeyboardOnTap:(id)sender
    {
         [[self view] endEditing:YES];
    }

You may need to double-click or long press using a similar technique. You may need to set the ViewController to UITextViewDelegate and connect the TextView to the ViewController. This method is applicable to UITextView and UITextField.

Source: Big Nerd Ranch

Edit: let me also add that if you are using UIScrollView, the above technologies may not work easily through Interface Builder. In this case, you can use UIGestureRecognizer and invoke the [[self view] endEditing:YES] method in it. An example is:

-(void)ViewDidLoad{
    ....
    UITapGestureRecognizer *tapRec = [[UITapGestureRecognizer alloc] 
        initWithTarget:self action:@selector(tap:)];
    [self.view addGestureRecognizer: tapRec];
    ....
}

-(void)tap:(UITapGestureRecognizer *)tapRec{
    [[self view] endEditing: YES];
}

When the user taps on the keyboard without tapping enter space, the keyboard closes.

#4 building

I know this is not the exact answer to this question, but after searching for the answer on the Internet, I found this topic. I think other people feel the same way.

This is the difference I found with UITapGestureRecognizer, which I found reliable and easy to use - just set TextView's delegation to ViewController.

When TextView becomes editable, I add UITapGestureRecognizer instead of ViewDidLoad:

-(void)textViewDidBeginEditing:(UITextView *)textView{
    _tapRec = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];

    [self.view addGestureRecognizer: _tapRec];
    NSLog(@"TextView Did begin");
}

When I click outside of TextView, the view will end editing mode, and UITapGestureRecognizer will delete itself, so I can continue to interact with other controls in the view.

-(void)tap:(UITapGestureRecognizer *)tapRec{
    [[self view] endEditing: YES];
    [self.view removeGestureRecognizer:tapRec];
    NSLog(@"Tap recognized, tapRec getting removed");
}

I hope it helps. It seems so obvious, but I've never seen this solution anywhere on the network - did I do anything wrong?

#5 building

My hack:

1 - create a button that covers the whole view; 2 - send it to the background of the view; 3 - change its type from "rounded rectangle" to "custom" in the property inspector; 4 - create operation 5, and implement the operation method:

- (IBAction)bgTouched:(id)sender 
{
    //to dismiss keyboard on bg btn pressed
    [_userInput resignFirstResponder];
}

_userInput is your TextField exit

Tags: network

Posted on Fri, 14 Feb 2020 04:12:11 -0500 by YOUAREtehSCENE