Apple does not provide placeholder functionality for Uitextview. We can do it in two ways.
Method One:
Idea: Set the default display of text, the color is set to gray. The agent method listens for the number of words TextView start editing and ending editing.
Cons: If you click on the text, the cursor will not appear at the beginning of the TextView. different from native placeholder.
1 _contenttext = [[Uitextview alloc] init]; 2 @" Please enter your feedback " ; 3 _contenttext.textcolor = [Uicolor graycolor]; 4 _contenttext. delegate = self;
Compliance <uitextviewdelegate> Implementation methods
1- (void) Textviewdidendediting: (Uitextview *) TextView2 {3 if(TextView.text.length <1){4Self.contentText.text =@"Please enter your feedback";5Self.contentText.textColor =[Uicolor Graycolor];6 }7 }8- (void) Textviewdidbeginediting: (Uitextview *) TextView9 {Ten if([Self.contentText.text isequaltostring:@"Please enter your feedback"]){ Oneself.contenttext.text=@""; ASelf.contenttext.textcolor=[Uicolor Blackcolor]; - } -}
Method Two:
Idea: Add a Uilabel to the TextView. Put it in the right place. Listen for input box text change, change the alpha of the label
1 #pragma mark-Listen input box 2 -(void) Textviewdidchange: (Uitextview *) textview{3 if (! Self.contentText.text.length) {4 1; 5 } Else {6 0 ; 7 }8 }
IOS adds a placeholder to Uitextview