64

I would like to get rid of the "return" function of the keyboard while the user is typing, so there are no new lines, so instead I would like the 'return' key to function as 'Done' so it would hide the keyboard.

I am using a UITextView, that is editable, so the user is able to type their post, and post it to the main timeline, but since I have fixed cells, I don't want the user to be able to press 'return' and their post would be out of range of the timeline.

I found this that works with UITextField, but not with UITextView:

func textFieldShouldReturn(textField: UITextField!) -> Bool {
    textField.resignFirstResponder()  //if desired
    return true
}

So I just wanted to know if there is a way to do that in a UITextView, or at least to be able to hide the keyboard if pressed return, instead of creating a new line.

4 Answers 4

142

You can set the return key type of the text field:

textField.returnKeyType = UIReturnKeyType.done

Update You can definitely use the same approach to set the return key to "Done", as mentioned above. However, UITextView doesn't provide a callback when user hits the return key. As a workaround, you can try to handle the textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) delegate call, and dismiss the keyboard when you detect the input of a new line character:

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
    if (text == "\n") {
        textView.resignFirstResponder()
    }
    return true
}
4
  • This replaced the function of the "Return" button to hide keyboard. It doesn't post any more new lines. Thank you! Commented Aug 13, 2015 at 1:03
  • 1
    The code in shouldChangeTextInRange is only going to work if a user is typing one character at a time. It will fail if a user pastes in multiple lines of text.
    – rmaddy
    Commented Mar 20, 2018 at 0:48
  • @utogaria How UITextView delegate will get called for UITextField ? Commented Apr 11, 2018 at 9:23
  • 4
    Minor tweak: return false if a new line was entered, if you don't want the new line to be stored
    – wardw
    Commented Aug 4, 2018 at 13:09
8

I have tried many codes and finally this worked for me in Swift 3.0 Latest [April 2019] this achieved using UITextFields

The "ViewController" class should be inherited the "UITextFieldDelegate" for making this code working.

class ViewController: UIViewController,UITextFieldDelegate  

Add the Text field with the Proper Tag number and this tag number is used to take the control to appropriate text field based on incremental tag number assigned to it.

override func viewDidLoad() {

    userNameTextField.delegate = self
    userNameTextField.tag = 0
    userNameTextField.returnKeyType = .next
    passwordTextField.delegate = self
    passwordTextField.tag = 1
    passwordTextField.returnKeyType = .go
}

In the above code, the "returnKeyType = UIReturnKeyType.next" where will make the Key pad return key to display as "Next" you also have other options as "Join/Go" etc, based on your application change the values.

This "textFieldShouldReturn" is a method of UITextFieldDelegate controlled and here we have next field selection based on the Tag value incrementation.

func textFieldShouldReturn(_ textField: UITextField) -> Bool
{
    if let nextField = textField.superview?.viewWithTag(textField.tag + 1) as? UITextField {
        nextField.becomeFirstResponder()
    } else {
        textField.resignFirstResponder()
        return true;
    }
    return false
}
3
  • @Sanf0rd: I appreciate and agree with your edit to remove "UIReturnKeyType" class name and just use as ".go" or ".next", since they are static property. I explicitly used to help readers to know about that class "UIReturnKeyType" we used, so they can go and check other available property options [google, yahoo, send, emergencyCall etc...] in the class. Is that fine or you feel to remove the class name. Commented Feb 22, 2018 at 20:55
  • 1
    This answer is for UITextFields, but the question is about UITextView
    – JAB
    Commented Mar 2, 2018 at 3:34
  • Yes, This is achieved using UITextFields only, which is an alternate option I have tried and working. Commented Mar 6, 2018 at 15:03
5

If you're working with a storyboard or xib, you can change the UITextView's Return button to 'Done' (or various other options) within Interface Builder, without the need for any setup code. Just look for this option in the Attributes inspector:

Return Key option in Interface Builder

From there, you just pair it up with the UITextViewDelegate code that others have already provided here.

Swift v5:

extension ExampleViewController: UITextViewDelegate {

    func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        if (text == "\n") {
            textView.resignFirstResponder()
        }
        return true
    }
}

And then, in your viewDidLoad() method:

exampleTextView.delegate = self

1
  • If your textView is programatic use: yourTextView.returnKeyType = .done. You can add that in viewDidLoad Commented May 10, 2020 at 8:14
0

Working in Swift 4

Add this in viewDidLoad().

textField.returnKeyType = UIReturnKeyType.Done

Add this anywhere you like.

extension UITextView: UITextViewDelegate {
    public func textViewDidChange(_ textView: UITextView) {
        if text.last == "\n" { //Check if last char is newline
            text.removeLast() //Remove newline
            textView.resignFirstResponder() //Dismiss keyboard
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.