Why UITextview does not have placeholder property like UITextField
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.
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.
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);
}
Mahesh Peri
Updated on December 12, 2020Comments
-
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 about 11 yearsThank you very much. Excellent answer...
-
tcd over 10 yearsgreat answer! note: if you have
[txtView becomeFirstResponder]
in yourviewWillAppear
, remove thetextViewDidBeginEditing
or setself.defaultLabel.hidden = NO;
. Thanks! -
Fabio Russo over 10 yearsWorks like a charm. Thanks
-
Hunkpapa over 10 yearsPerfect! 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 over 10 yearsawesome ! 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 about 10 yearsI 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 over 9 yearsYou probably mean UITextView in your last line: Apple also uses placeholder text in some places where it uses a UITextView.
-
Davit Siradeghyan about 9 yearsvery nice workaround