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

1

10

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?

Freida answered 13/8, 2009 at 17:27 Comment(2)
MacRumors has some code you can try.Hesperides
No. In the link, that you gave to me is about "textField", I nee Answer About "textView"Freida
M
17

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
}
Mangrove answered 13/8, 2009 at 17:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.