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;
}
Author by
ios
Updated on October 26, 2020Comments
-
ios over 3 years
Possible Duplicate:
Placeholder in UITextViewIn iPhone App How to add placeHolder Text (to hold some default text) in
UItextView
? -
Sugan S over 11 yearsI 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 over 11 yearsHai @sugan.s I guess you are not setting up the text color as '[itsTextView setTextColor:[UIColor lightGrayColor]];'
-
Dilip Rajkumar over 11 yearsAs you can see we are clearing the text based on the text color.. let me know if it helps..
-
Sugan S over 11 yearsshall i include code here will you help me
-
Dilip Rajkumar over 11 yearsHai 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 over 11 yearsThanks Dilip ill send to your mail ID whenever you feel free help me.
-
Dilip Rajkumar over 11 yearsI got your email, I ill check and reply you.
-
Sugan S over 11 yearsThank you so much friend
-
Fahim Parkar almost 11 yearsI think this is best idea for adding placeholder in UITextView...
-
Brett Donald about 9 yearsI 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 about 7 years