Enabling the done button on a UITextView
Are you sure that you want to do that?
The following post explains how to make sure you are “done” when the user has finished entering their text in a UITextView
- Some understanding of testing using Xcode
UITextView: An object that displays text in a custom style while supporting text editing
When pressing the return button you want the keyboard to disappear.
You will need to make your ViewController conform to the UITextViewDelegate protocol; this will look something like:
Then implement the following within your View Controller
and of course tell your textView that the view controller is the delegate
but Wait — do you want to do that?
By implementing this you prevent your user from being able to enter spaces within their TextView! I’m not sure this is something you want to prevent them from doing.
Better functionality would be to add a done button in the toolbar. Something like the following would do the job:
Perhaps the best way to do this is through an extension. I called this file UIViewController — hideKeyboardOnBackgroundTouched.swift
This can then be called by placing the following in a appropriate place (perhaps in ViewDidLoad, which is where I placed this code snippet):
Where of course myTextView is replaced with the name of your text view.
Want to get in contact? Try the link here: