0

I have used two delegate methods to create a place holder for textView.

Code:

-(BOOL)textViewShouldBeginEditing:(UITextView *)textView
{

    if([Description.text isEqualToString:@"Description of home"]&&[Description.font isEqual:[UIFont fontWithName:@"Helvetica" size:18]])
    {
        Description.text=nil;
    }
    if([Display.text isEqualToString:@"Display"]&&[Display.font isEqual:[UIFont fontWithName:@"Helvetica" size:18]])
    {
        Display.text=nil;
    }
    return YES;
}
-(BOOL)textViewShouldEndEditing:(UITextView *)textView
{

    if ([Description.text isEqualToString:@""]) // Description is textViewObject
    {
        Description.text=@"Description of home";
        Description.font=[UIFont fontWithName:@"Helvetica" size:18];
    }


    if ([Display.text isEqualToString:@""])
    {
        Display.text=@"Display";   // Display is textViewObject
        Display.font=[UIFont fontWithName:@"Helvetica" size:18];
    }
    return YES;
}
@end

This is working correct. But problem is that when I am clicking on any textView, the text is getting nil from all textView. This is because I have connected all textView to delegate.

I want that text should disappear on particular textView only.

Bhupesh Kumar
  • 369
  • 2
  • 18
  • possible duplicate of [Placeholder in UITextView](http://stackoverflow.com/questions/1328638/placeholder-in-uitextview) – uraimo Jul 22 '15 at 06:56
  • I think the best way to add placeholder support to UITextView is to subclass it, like this: [YXTextView](https://github.com/yixia-team/YXTextView). – Allen Jul 22 '15 at 06:57
  • You need to subclass UITextView and add a label as a subview. You will drive yourself crazy doing it this way through delegate methods. – DBoyer Jul 22 '15 at 07:15

4 Answers4

2

you could add conditions in the delegate methods like this :

-(BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
if(textView == yourTextView1)
{
    //your code for perticular textView1
}
 else if(textView == yourTextView2)
{
    //your code for perticular textView2
}

  return YES;
}
iAnurag
  • 9,286
  • 3
  • 31
  • 48
0

You need to differentiate each of your UITextViews in some way.

If you are to have a small number of UITextViews on the screen, then you should save each of your UItextViews as a property. Check out this youtube video on creating IBOutlets

Then just name your UITextViews in order: textView1, textView2 etc...

Then in your delegate methods, you can check if the textView being passed in the method is equal to the property you want to delete text from.

if (textView == self.textView1) {
    //delete text
} else {
    //do nothing or something else
}
Adama
  • 1,101
  • 8
  • 26
-1

check if the delegate is operating on the wanted UITextView:

-(BOOL)textViewShouldBeginEditing:(UITextView *)textView
{

    if(textView == Description && [Description.text isEqualToString:@"Description of home"]&&[Description.font isEqual:[UIFont fontWithName:@"Helvetica" size:18]])
    {
        Description.text=nil;
    }
    if(textView==Display && [Display.text isEqualToString:@"Display"]&&[Display.font isEqual:[UIFont fontWithName:@"Helvetica" size:18]])
    {
        Display.text=nil;
    }
    return YES;
}
Yedidya Reiss
  • 5,316
  • 2
  • 17
  • 19
-1

Easiest solution by @ CmKndy for this problem is https://stackoverflow.com/a/10201671/3633534

use UITextViewDelegate delegate in your class and add text @"placeholder text here..." in your textView with lightGrayColor to display by default.

Following code is for place holder in textView :

- (void)textViewDidBeginEditing:(UITextView *)textView
{
    if ([textView.text isEqualToString:@"placeholder text here..."]) {
        textView.text = @"";
        textView.textColor = [UIColor blackColor]; //optional
    }
    [textView becomeFirstResponder];
}

- (void)textViewDidEndEditing:(UITextView *)textView
{
    if ([textView.text isEqualToString:@""]) {
        textView.text = @"placeholder text here...";
        textView.textColor = [UIColor lightGrayColor]; //optional
    }
    [textView resignFirstResponder];
}

For your question[Update]

-(BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    if(textView == Description && [Description.text isEqualToString:@"Description of home"])
    {
        Description.text = @"";
    }
    else if(textView == Display && [Display.text isEqualToString:@"Display"])
    {
        Display.text = @"";
    }
    return YES;
}

-(BOOL)textViewShouldEndEditing:(UITextView *)textView
{
    if (textView == Description && [Description.text isEqualToString:@""]) // Description is textViewObject
    {
        Description.text = @"Description of home";
        Description.font=[UIFont fontWithName:@"Helvetica" size:18];
    }
    else if (textView == Display && [Display.text isEqualToString:@""])
    {
        Display.text = @"Display";   // Display is textViewObject
        Display.font=[UIFont fontWithName:@"Helvetica" size:18];
    }
    return YES;
}
@end
Community
  • 1
  • 1
Sujay
  • 2,510
  • 2
  • 27
  • 47
  • You could do this, but it's unconventional. Creating an IBOutlet property referencing the UITextView is safer and more predictable than matching strings. You could type something wrong in the "if statement" or in the placeholder field in IB, and it might take more time to debug. – Adama Jul 22 '15 at 07:04
  • 1
    @Adama, `(UITextView *)textView` will always reference to current `textView` and scope is only in defined method, so their is no chance of mistake. `IBOutlet` is different thing, it is for global use to refer any `id`. – Sujay Jul 22 '15 at 07:14