Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Another easy solution is to just add a <code>UILabel</code> to your <code>UITextView</code> subview.</p> <pre><code>- (void)viewDidLoad { [super viewDidLoad]; // you might have to play around a little with numbers in CGRectMake method // they work fine with my settings placeholderLabel = [[UILabel alloc] initWithFrame:CGRectMake(10.0, 0.0, textView.frame.size.width - 20.0, 34.0)]; [placeholderLabel setText:kDescriptionPlaceholder]; // placeholderLabel is instance variable retained by view controller [placeholderLabel setBackgroundColor:[UIColor clearColor]]; [placeholderLabel setFont:[challengeDescription font]]; [placeholderLabel setTextColor:[UIColor lightGrayColor]]; // textView is UITextView object you want add placeholder text to [textView addSubview:placeholderLabel]; } - (void) textViewDidChange:(UITextView *)theTextView { if(![textView hasText]) { [textView addSubview:placeholderLabel]; } else if ([[textView subviews] containsObject:placeholderLabel]) { [placeholderLabel removeFromSuperview]; } } - (void)textViewDidEndEditing:(UITextView *)theTextView { if (![textView hasText]) { [textView addSubview:placeholderLabel]; } } </code></pre> <p>You can even add little animations to fade in/out the <code>UILabel</code> if that's your thing.</p> <pre><code>- (void) textViewDidChange:(UITextView *)theTextView { if(![textView hasText]) { [textView addSubview:placeholderLabel]; [UIView animateWithDuration:0.15 animations:^{ placeholderLabel.alpha = 1.0; }]; } else if ([[textView subviews] containsObject:placeholderLabel]) { [UIView animateWithDuration:0.15 animations:^{ placeholderLabel.alpha = 0.0; } completion:^(BOOL finished) { [placeholderLabel removeFromSuperview]; }]; } } - (void)textViewDidEndEditing:(UITextView *)theTextView { if (![textView hasText]) { [textView addSubview:placeholderLabel]; [UIView animateWithDuration:0.15 animations:^{ placeholderLabel.alpha = 1.0; }]; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload