16

Possible Duplicate:
Placeholder in UITextView

In iPhone App How to add placeHolder Text (to hold some default text) in UItextView?

Community
  • 1
  • 1
ios
  • 6,134
  • 20
  • 71
  • 103

1 Answers1

52

Simple, I did it this way.. working great for me.. Hope this helps some one..

#pragma mark -
#pragma mark TextView Delegate methods


    UITextView itsTextView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, itsTextView.frame.size.width, itsTextView.frame.size.height)];
            [itsTextView setDelegate:self];
            [itsTextView setReturnKeyType:UIReturnKeyDone];
            [itsTextView setText:@"List words or terms separated by commas"];
            [itsTextView setFont:[UIFont fontWithName:@"HelveticaNeue" size:11]];
            [itsTextView setTextColor:[UIColor lightGrayColor]];

- (BOOL) textViewShouldBeginEditing:(UITextView *)textView
{
    if (itsTextView.textColor == [UIColor lightGrayColor]) {
        itsTextView.text = @"";
        itsTextView.textColor = [UIColor blackColor];
    }

    return YES;
}

-(void) textViewDidChange:(UITextView *)textView
{
    if(itsTextView.text.length == 0){
        itsTextView.textColor = [UIColor lightGrayColor];
        itsTextView.text = @"List words or terms separated by commas";
        [itsTextView resignFirstResponder];
    }
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {

    if([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        if(itsTextView.text.length == 0){
            itsTextView.textColor = [UIColor lightGrayColor];
            itsTextView.text = @"List words or terms separated by commas";
            [itsTextView resignFirstResponder];
        }
        return NO;
    }

    return YES;
}
Dilip Rajkumar
  • 7,006
  • 6
  • 60
  • 76
  • I included above code but while didbegin editing it clears the placeholder but in viewdidchange it always place the placeholder so user not able to edit/type in textview is there any solution – Sugan S Sep 18 '12 at 13:01
  • Hai @sugan.s I guess you are not setting up the text color as '[itsTextView setTextColor:[UIColor lightGrayColor]];' – Dilip Rajkumar Sep 18 '12 at 13:10
  • As you can see we are clearing the text based on the text color.. let me know if it helps.. – Dilip Rajkumar Sep 18 '12 at 13:11
  • shall i include code here will you help me – Sugan S Sep 20 '12 at 04:28
  • Hai Sugan, please send me the code to dilip.id@me.com ill try to help or you can post it as new question and give the link here.. – Dilip Rajkumar Sep 20 '12 at 18:43
  • Thanks Dilip ill send to your mail ID whenever you feel free help me. – Sugan S Sep 21 '12 at 05:10
  • I got your email, I ill check and reply you. – Dilip Rajkumar Sep 21 '12 at 10:53
  • I think this is best idea for adding placeholder in UITextView... – Fahim Parkar Jul 09 '13 at 07:19
  • I like this solution, except for the fact that I then can't just stick textView.text into the database, I have to test whether or not it matches the prompt. So I based my solution on yours, implementing the same delegate methods, but instead of altering the text in the textView, I show/hide a label which is superimposed on top of the textView. It seems neater to me. – Brett Donald Feb 23 '15 at 23:25
  • http://stackoverflow.com/questions/1328638/placeholder-in-uitextview – Hardik1344 Apr 19 '17 at 06:46