How to add placeHolder Text in UItextView? in iphone sdk

48,952

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;
}
Share:
48,952
ios
Author by

ios

Updated on October 26, 2020

Comments

  • ios
    ios over 3 years

    Possible Duplicate:
    Placeholder in UITextView

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

  • Sugan S
    Sugan S over 11 years
    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
  • Dilip Rajkumar
    Dilip Rajkumar over 11 years
    Hai @sugan.s I guess you are not setting up the text color as '[itsTextView setTextColor:[UIColor lightGrayColor]];'
  • Dilip Rajkumar
    Dilip Rajkumar over 11 years
    As you can see we are clearing the text based on the text color.. let me know if it helps..
  • Sugan S
    Sugan S over 11 years
    shall i include code here will you help me
  • Dilip Rajkumar
    Dilip Rajkumar over 11 years
    Hai Sugan, please send me the code to [email protected] ill try to help or you can post it as new question and give the link here..
  • Sugan S
    Sugan S over 11 years
    Thanks Dilip ill send to your mail ID whenever you feel free help me.
  • Dilip Rajkumar
    Dilip Rajkumar over 11 years
    I got your email, I ill check and reply you.
  • Sugan S
    Sugan S over 11 years
    Thank you so much friend
  • Fahim Parkar
    Fahim Parkar almost 11 years
    I think this is best idea for adding placeholder in UITextView...
  • Brett Donald
    Brett Donald about 9 years
    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.
  • Hardik1344
    Hardik1344 about 7 years