Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide keyboard - of - UITextView iPhone - by return Key [duplicate]

In a UITextView, when we click on it,

A keyboard appears,

but when user press return key, (normally creates a new line in textView)

keyboard should go down.

How?

like image 425
Sagar Kothari Avatar asked Aug 13 '09 17:08

Sagar Kothari


People also ask

How do you dismiss a keyboard on the return key in Swift?

If you have a UITextField and you want to hide the keyboard when the user is pressing the return key, use the textFieldShouldReturn function. Be sure you have set the textField. delegate = self in your viewDidLoad() function.

What is UITextField?

An object that displays an editable text area in your interface.


1 Answers

Ok i have found the correct answer by the help of @jordan - link help.

Implement following code to your view controller .m file & .h file add delegate

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

Now goto interface builder, select your textview & set return key type done.

Every thing works fine & great.

I have implemented it.

For Swift:

func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {

    if text == "\n"{
      //do stuff
      return false
    }
    return true
}

For swift 3:

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {

    if text == "\n"{
        //do stuff
        return false
    }
    return true
}
like image 191
4 revs, 3 users 64% Avatar answered Nov 16 '22 00:11

4 revs, 3 users 64%