Add placeholder text inside UITextView in Swift?

StevenR picture StevenR · Dec 26, 2014 · Viewed 264.6k times · Source

How can I add a placeholder in a UITextView, similar to the one you can set for UITextField, in Swift?

Answer

Lyndsey Scott picture Lyndsey Scott · Dec 26, 2014

Updated for Swift 4

UITextView doesn't inherently have a placeholder property so you'd have to create and manipulate one programmatically using UITextViewDelegate methods. I recommend using either solution #1 or #2 below depending on the desired behavior.

Note: For either solution, add UITextViewDelegate to the class and set textView.delegate = self to use the text view’s delegate methods.


Solution #1 - If you want the placeholder to disappear as soon as the user selects the text view: