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

我怕爱的太早我们不能终老 提交于 2020-01-12 04:49:11

问题


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?


回答1:


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
}


来源:https://stackoverflow.com/questions/1273439/how-to-hide-keyboard-of-uitextview-iphone-by-return-key

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