I have 2 UITextViews on a single View Controller. They have placeholders in each and I'm trying to get the placeholder to disappear when the textViewShouldBeginEditing. I've tried using tags, but the first one will work, then each selection after that fails to destroy the placeholders. Thanks
Asked
Active
Viewed 46 times
-2
-
placeholder? in a textview? there isn't. – Matteo Gobbi Feb 04 '15 at 20:33
-
It would help if you could show all relevant code. The placeholder also automatically clears once you start typing. – CaptJak Feb 04 '15 at 20:33
-
I had to create NSMutableAttributedStrings for the place holders as they have '>' in them. – TomG103 Feb 04 '15 at 20:41
-
is there textField or TextView. UITextView doesn't have placeholder. – Shashi3456643 Feb 04 '15 at 20:41
-
Do you want both to disappear when one textView is selected, or indiviually? – hidden-username Feb 04 '15 at 20:43
-
I want the placeholders to be empty once the textview has been selected. So one at a time. – TomG103 Feb 04 '15 at 20:44
-
It's a TextView with NSMutableAttributedStrings in it. So self.textview.attributedText = self.NSMutableAttributedString – TomG103 Feb 04 '15 at 20:44
-
Not sure why someone down voted this post. Assigning UITextView with an attributedText gives it a place holder. – TomG103 Feb 04 '15 at 20:57
-
textView.text containsString, really is not a good way to handle this. What if i type something with "enter" dismiss, and then reenter the textView. Is that the same place you tried if [textView is firstResponder()] instead of contains string? – hidden-username Feb 04 '15 at 21:21
3 Answers
2
just do like this
-(void)textViewDidBeginEditing:(UITextView *)textView
{
if([textView.text isEqualToString:@"Enter Text here......"]) //Enter Text here...... is your placeholder text
{
textView.text = @"";
}
}
- (void)textViewDidEndEditing:(UITextView *)textView
{
if([textView.text length] == 0)
{
textView.text = @"Enter Text here......";
}
}

Noor
- 2,071
- 19
- 28
-
Normally I would do that. The issue for me is that they have different placeholders of NSMutableAttributedString types. – TomG103 Feb 04 '15 at 20:38
-
I also like putting this in shouldChange textInRange if textView.text.length == 1 && text == "" { textView.attributedText = textView.placeHolder textView.selectedRange = NSMakeRange(0, 0) – hidden-username Feb 04 '15 at 20:40
0
Instead of using
-(BOOL)textViewShouldBeginEditing:(UITextView *)textView
I used
-(void)textViewDidBeginEditing:(UITextView *)textView
and it works fine now. Thanks for the help all.

TomG103
- 311
- 2
- 26
-2
Use if textView.isFirstResonder, but do it in
textViewDidBeginEditing
not in
textViewShouldBeginEditing

hidden-username
- 2,610
- 3
- 14
- 19
-
This doesn't classify as an answer. If another answer is incorrect, state that it is so that it can be removed or fixed. The purpose is to provide clear correct answers for not only the OP, but also for any others who might have similar problems – CaptJak Feb 04 '15 at 20:55
-
That's not working either. I tried using tags and if tag = 1 else if tag = 2 and that fails as well. – TomG103 Feb 04 '15 at 20:56