Why UITextview does not have placeholder property like UITextField

20,463

Solution 1

Why UITextField has a placeholder value and UITextView doesn't has nothing to do with with their inheritance, or even programming. It's about how Apple perceived their use and prefers their use.

UITextField is a one line item. It can be used for entering usernames, passwords, address bits, or pretty much anything. Often you have several of them clumped together, and it might be hard to figure out which field should have which data without placeholder text. The place holder lets us know the purpose of the field.

Text field example

UITextView is a block of text. Usually several lines, which often have a header associated with them. As a result, placeholder text is often not useful. The text view is also more likely to be populated with static text which again, would make placeholder text not really useful.

Do I think Apple should add a placeholder property for UITextView anyway? Yes. It's annoying to have to make my own on the occasion when I need it. Apple also uses placeholder text in some places where it uses a UITextView. The following example is from adding a calendar entry.

Text view example with placeholder

Solution 2

I don't know why Apple did not include a placeHolder attribute on UITextView. But I was able to add one using a UILabel subview that contains the placeholder text.

Then show or hide it from delegate callbacks as below

- (void)textViewDidBeginEditing:(UITextView *)textView
{
    self.defaultLabel.hidden = YES;
}

- (void)textViewDidChange:(UITextView *)txtView
{
    self.defaultLabel.hidden = ([txtView.text length] > 0);
}

- (void)textViewDidEndEditing:(UITextView *)txtView
{
    self.defaultLabel.hidden = ([txtView.text length] > 0);
}
Share:
20,463
Mahesh Peri
Author by

Mahesh Peri

Updated on December 12, 2020

Comments

  • Mahesh Peri
    Mahesh Peri over 3 years

    Hi I am working with iPhone apps, I know the UITextfield has placeholder property. But UITextview doesn't have placeholder property. I know the process how to customize placeholder for UITextview. But please let me know the difference.

    and UITextfield inherits from UIControl which inturns inherits from UIView...NSObject. and UITextview inherits from UIScrollView, UIView,UIresponder...NSOBject. But where is the difference frome place holder property.

    How textfield contains placeholder property, why textview doesn't contain placeholder?

    Please give any ideas, suggestions, and/or give better explanation.

  • Mahesh Peri
    Mahesh Peri about 11 years
    Thank you very much. Excellent answer...
  • tcd
    tcd over 10 years
    great answer! note: if you have [txtView becomeFirstResponder] in your viewWillAppear, remove the textViewDidBeginEditing or set self.defaultLabel.hidden = NO;. Thanks!
  • Fabio Russo
    Fabio Russo over 10 years
    Works like a charm. Thanks
  • Hunkpapa
    Hunkpapa over 10 years
    Perfect! And if you preload the textview when showing your controller, with some value that the user saved earlier, don't forget to add self.defaultLabel.hidden = ([txtView.text length] > 0); also after the preload code in viewWillAppear, otherwise the placeholder will be visible on top of the loaded text
  • GuramK
    GuramK over 10 years
    awesome ! just little note, if you want this to behave like UITextField placeholder, remove - (void)textViewDidBeginEditing:(UITextView *)textView. Placeholder should disappear when user actually starts typing.
  • Scott Carter
    Scott Carter about 10 years
    I liked this answer, but wanted to reference another thread: stackoverflow.com/questions/1824463/… See the answer there by Phil, Dec 20 '12. That answer showed how to position a UITextField under a UITextView. I then controlled the placeholder text by looking at whether there were any characters typed. I did this in viewDidLoad & textViewDidChange.
  • jomafer
    jomafer over 9 years
    You probably mean UITextView in your last line: Apple also uses placeholder text in some places where it uses a UITextView.
  • Davit Siradeghyan
    Davit Siradeghyan about 9 years
    very nice workaround