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

前端 未结 4 2029
夕颜
夕颜 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: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

提交回复
热议问题