Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding default text to UITextView

I was wondering how to display a default prompt in a UITextView. If I wanted the user to type a description in a text view, the UITextView could have "description" printed in it, and when the user starts to type, it disappears.

like image 777
matthew Avatar asked Jan 12 '23 07:01

matthew


2 Answers

For UITextField, there is the placeholderText property, which will display a grayed out text that is removed once the user starts typing.

For UITextView, you can use a custom implementation, such as SZTextView, which implements a similar functionality of a placeholder text.

like image 187
Léo Natan Avatar answered Jan 18 '23 23:01

Léo Natan


It wont be a wise idea to use a third party uitextview for placeholder property. Follow these steps and you will achieve what you need-

    set- textview.textcolor=[uicolor greycolor];
    textview.text=@"Your initial placeholder text";

Now in -textViewShouldBeginEditing write these lines of codes-

    if(textview.color==[uicolor greycolor]){
         textview.color=[uicolor blackcolor];
         textview.text=@"";

    }

Cheers.

like image 40
Anubrata Santra Avatar answered Jan 18 '23 21:01

Anubrata Santra