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

前端 未结 4 2030
夕颜
夕颜 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 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
    }
    

提交回复
热议问题