How to insert Placeholder in UITextView

Placeholder is something that is required to help user understand what the control is for.

placeholder
placeholder

Placeholder

A string that’s displayed in the compose view’s text view when the text view is empty.

Declaration

var placeholder: String! { get set }

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

First set the UITextView to contain the placeholder text and set it to a light gray color to mimic the look of a UITextField’s placeholder text. Either do so in the viewDidLoad or upon the text view’s creation.

textView.text = “Placeholder”
textView.textColor = UIColor.lightGray

Solution #2 – If you want the placeholder to show whenever the text view is empty, even if the text view’s selected:

First set the placeholder in the viewDidLoad:

textView.text = “Placeholder”
textView.textColor = UIColor.lightGray

textView.becomeFirstResponder()

textView.selectedTextRange = textView.textRange(from: textView.beginningOfDocument, to: textView.beginningOfDocument)

 

Format

Objective-C code:

Swift 3

If your textField has text, you need to first set text property to nil, then set placeholder text:

Few Examples are below as follows

in OBJECTIVE-C

 

UIPlaceHolderTextView.h:

UIPlaceHolderTextView.m: