Using UITextView placeholder and not clearing text on re-entry

ios, ios7, objective-c, uitextview, xcode

Solution

For having a placeholder functionality in `UITextView`, i generally do this:

[self.gcTextView setText:@"placeholder"];
[self.gcTextView setTextColor:[UIColor lightGrayColor]];
[self.gcTextView setTag:100]; //start tag with default 100
- (void) textViewShouldBeginEditing:(UITextView *) textView
{
    if(textView.tag == 100) {
        [textView setTag:200];
        [textView setText:@""];
        [textView setTextColor:[UIColor blackColor];
    }
}

- (void) textViewDidEndEditing:(UITextView *) textView
{
    //handle text that has spaces as it's content (i.e. no characters)
    NSString *strStrippedText = [textView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

    if(strStrippedText.length == 0) {
        [textView setTag:100];
        [textView setText:@"Placeholder"];
        [textView setTextColor:[UIColor lightGrayColor];
    }
}

Basically, it's all about the tag I set to it

EDIT: However...

If you use tags for other purposes then this might break something so you can safely modify the `bounces` property and use it as an indicator for your personal use.

[self.gcTextView setText:@"placeholder"];
[self.gcTextView setTextColor:[UIColor lightGrayColor]];
[self.gcTextView setBounces:NO]; //NO for placeholder text
- (void) textViewShouldBeginEditing:(UITextView *) textView
{
    if(textView.bounces == NO) {
        [textView setText:@""];
        [textView setTextColor:[UIColor blackColor];
        [textView setBounces:YES]; //YES for non-placeholder text
    }
}

- (void) textViewDidEndEditing:(UITextView *) textView
{
    //handle text that has spaces as it's content (i.e. no characters)
    NSString *strStrippedText = [textView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

    if(strStrippedText.length == 0) {
        [textView setText:@"Placeholder"];
        [textView setTextColor:[UIColor lightGrayColor];
        [textView setBounces:NO];
    }
}

Problem

I'd like to have a placeholder in my UITextView and I'd rather avoid complexities like dragged out methods(overloaded code) and subclassing. I just need to retain edit typing. I'm currently using this methodology to create a placeholder in an `UITextView` .h ``` @property (weak, nonatomic) IBOutlet UITextView *gcTextView; ``` .m ``` @synthesize gcTextView; ``` add this to to `viewDidLoad:` ``` self.gcTextView.text = @"placeholder text here"; self.gcTextView.textColor = [UIColor lightGrayColor]; gcTextView.layer.borderColor = [[UIColor whiteColor] CGColor]; ``` Then use this method for editing-> ``` - (void) textViewDidBeginEditing:(UITextView *) textView { [textView setText:@""]; self.gcTextView.textColor = [UIColor blackColor]; } ``` QUESTION: What will I need to add to this way of doing it to 1) add the placeholder back to the `textView` if edited text returns to nil or `@""` I guess. 2) Not delete all the user's edited text when re-entering edit mode on the `textView`

Original source