Change 'Return' button function to 'Done' in swift in UITextView

前端 未结 4 2025
夕颜
夕颜 2021-01-30 10:50

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

相关标签:
4条回答
  • 2021-01-30 10:51

    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
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-30 11:06

    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
    }
    
    0 讨论(0)
  • 2021-01-30 11:07

    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
    }
    
    0 讨论(0)
  • 2021-01-30 11:07

    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:

    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

    0 讨论(0)
提交回复
热议问题