Why UITextview does not have placeholder property like UITextField

Mahesh Peri picture Mahesh Peri · Apr 8, 2013 · Viewed 20.4k times · Source

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.

Answer

sixthcent picture sixthcent · Apr 8, 2013

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);
}