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

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

提交回复
热议问题