Placeholder in UITextView

And the stack overflow url is :http://stackoverflow.com/questions/1328638/placeholder-in-uitextview
Easy way, just create placeholder text in UITextView by using the following UITextViewDelegate methods:

  • (void)textViewDidBeginEditing:(UITextView *)textView
    {
    if ([textView.text isEqualToString:@“placeholder text here…“]) {

     textView.text = @"";
     textView.textColor = [UIColor blackColor]; //optional
    

    }
    [textView becomeFirstResponder];
    }

  • (void)textViewDidEndEditing:(UITextView *)textView
    {
    if ([textView.text isEqualToString:@““]) {

    textView.text = @"placeholder text here...";
    textView.textColor = [UIColor lightTextColor]; //optional
    

    }
    [textView resignFirstResponder];
    }
    just remember to set myUITextView with the exact text on creation e.g.

UITextView *myUITextView = [[UITextView alloc] init];
myUITextView.delegate = self;
myUITextView.text = @“placeholder text here…“;
myUITextView.textColor = [UIColor lightTextColor]; //optional
and make the parent class a UITextView delegate before including these methods e.g.

@interface MyClass ()
@end

你可能感兴趣的:(Placeholder in UITextView)