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
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