21

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.

icodebuster
  • 8,890
  • 7
  • 62
  • 65
Mahesh Peri
  • 1,689
  • 2
  • 14
  • 24

2 Answers2

52

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);
}
sixthcent
  • 1,150
  • 7
  • 6
  • great answer! note: if you have `[txtView becomeFirstResponder]` in your `viewWillAppear`, remove the `textViewDidBeginEditing` or set `self.defaultLabel.hidden = NO;`. Thanks! – tcd Aug 20 '13 at 19:27
  • 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 – Hunkpapa Sep 30 '13 at 20:04
  • 4
    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. – GuramK Jan 07 '14 at 11:59
  • I liked this answer, but wanted to reference another thread: http://stackoverflow.com/questions/1824463/how-to-style-uitextview-to-like-rounded-rect-text-field 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. – Scott Carter Apr 10 '14 at 19:56
  • very nice workaround – Davit Siradeghyan Apr 02 '15 at 19:47
27

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

Community
  • 1
  • 1
DBD
  • 23,075
  • 12
  • 60
  • 84
  • You probably mean UITextView in your last line: Apple also uses placeholder text in some places where it uses a UITextView. – jomafer Sep 02 '14 at 16:28