Jak wstawić element zastępczy w UITextView? [duplikat]

To pytanie ma już odpowiedź tutaj:

Czy Jest jakiś sposób, aby wstawić symbol zastępczy w UITextView podobny do UITextField? Jeśli tak, to prześlij mi link lub pomysł na wdrożenie tej funkcjonalności.

Author: Bartłomiej Semańczyk, 2011-08-12

6 answers

Nie jest możliwe utworzenie elementu zastępczego w UITextView, ale można wygenerować efekt jak uchwyt miejsca przez to.

    - (void)viewDidLoad{    
                  commentTxtView.text = @"Comment";
                  commentTxtView.textColor = [UIColor lightGrayColor];
                  commentTxtView.delegate = self;

         }
           - (BOOL) textViewShouldBeginEditing:(UITextView *)textView
         {
             commentTxtView.text = @"";
             commentTxtView.textColor = [UIColor blackColor];
             return YES;
         }

         -(void) textViewDidChange:(UITextView *)textView
         {

        if(commentTxtView.text.length == 0){
            commentTxtView.textColor = [UIColor lightGrayColor];
            commentTxtView.text = @"Comment";
            [commentTxtView resignFirstResponder];
        }
        }

-(void) textViewShouldEndEditing:(UITextView *)textView
         {

        if(commentTxtView.text.length == 0){
            commentTxtView.textColor = [UIColor lightGrayColor];
            commentTxtView.text = @"Comment";
            [commentTxtView resignFirstResponder];
        }
        }

Możesz też dodać etykietę w textview tak jak

       lbl = [[UILabel alloc] initWithFrame:CGRectMake(10.0, 0.0,textView.frame.size.width - 10.0, 34.0)];


[lbl setText:kDescriptionPlaceholder];
[lbl setBackgroundColor:[UIColor clearColor]];
[lbl setTextColor:[UIColor lightGrayColor]];
textView.delegate = self;

[textView addSubview:lbl];

And set

- (void)textViewDidEndEditing:(UITextView *) textView
{
     if (![textView hasText]) {
     lbl.hidden = NO;
}
}

- (void) textViewDidChange:(UITextView *)textView
{
    if(![textView hasText]) {
      lbl.hidden = NO;
}
else{
    lbl.hidden = YES;
 }  
}
 209
Author: PJR,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-02-24 11:19:27

Innym łatwym rozwiązaniem jest dodanie UILabel do subview UITextView.

- (void)viewDidLoad
{
  [super viewDidLoad];

  // you might have to play around a little with numbers in CGRectMake method
  // they work fine with my settings
  placeholderLabel = [[UILabel alloc] initWithFrame:CGRectMake(10.0, 0.0, textView.frame.size.width - 20.0, 34.0)];
  [placeholderLabel setText:kDescriptionPlaceholder];
  // placeholderLabel is instance variable retained by view controller
  [placeholderLabel setBackgroundColor:[UIColor clearColor]];
  [placeholderLabel setFont:[challengeDescription font]];
  [placeholderLabel setTextColor:[UIColor lightGrayColor]];

  // textView is UITextView object you want add placeholder text to
  [textView addSubview:placeholderLabel];
}

- (void) textViewDidChange:(UITextView *)theTextView
{
  if(![textView hasText]) {
    [textView addSubview:placeholderLabel];
  } else if ([[textView subviews] containsObject:placeholderLabel]) {
    [placeholderLabel removeFromSuperview];
  }
}

- (void)textViewDidEndEditing:(UITextView *)theTextView
{
  if (![textView hasText]) {
    [textView addSubview:placeholderLabel];
  }
}

Możesz nawet dodać małe animacje, aby zniknąć UILabel, jeśli to twoja rzecz.

- (void) textViewDidChange:(UITextView *)theTextView
{
  if(![textView hasText]) {
    [textView addSubview:placeholderLabel];
    [UIView animateWithDuration:0.15 animations:^{
      placeholderLabel.alpha = 1.0;
    }];
  } else if ([[textView subviews] containsObject:placeholderLabel]) {

    [UIView animateWithDuration:0.15 animations:^{
      placeholderLabel.alpha = 0.0;
    } completion:^(BOOL finished) {
      [placeholderLabel removeFromSuperview];
    }];
  }
}


- (void)textViewDidEndEditing:(UITextView *)theTextView
{
  if (![textView hasText]) {
    [textView addSubview:placeholderLabel];
    [UIView animateWithDuration:0.15 animations:^{
      placeholderLabel.alpha = 1.0;
    }];
}
 17
Author: jlajlar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-04-14 15:02:33

Innym prostym rozwiązaniem jest ustawienie tak / nie dla ukrytych właściwości placeholderLabel

- (void)textViewDidEndEditing:(UITextView *)theTextView
{
    if (![textView hasText]) {
        placeholderLabel.hidden = NO;
    }
}

- (void) textViewDidChange:(UITextView *)textView
{
    if(![textView hasText]) {
        placeholderLabel.hidden = NO;
    }
    else{
        placeholderLabel.hidden = YES;
    }
}
 9
Author: MingSoft,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-10-08 08:55:35
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
  // NSLog(@"REPlace %@ %d",text,range.location);
  if(range.location==0 && ![text isEqualToString:@""])
  {
    placeholderlbl.hidden = YES;
  }
  else if(range.location==0)
  {
    placeholderlbl.hidden = NO;
  }

  return YES;
}
 4
Author: swamy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-10-30 06:28:45

Możesz użyć poniższej funkcji do tworzenia zastępczych, za pomocą tego możesz ustawić na nim uchwyt miejsca..

[self addTextViewPlaceholder:self.txtvComment withPlaceholder:@"COMMENT"];

-(void) addTextViewPlaceholder:(UITextView*) tView withPlaceholder:(NSString*) placeholder
{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(9,8,tView.bounds.size.width - 16,0)];
    label.lineBreakMode = UILineBreakModeWordWrap;
    label.numberOfLines = 0;
    label.font = [UIFont systemFontOfSize:13.0];
    label.backgroundColor = [UIColor clearColor];
    label.textColor = [UIColor colorWithRed:0.7 green:0.7 blue:0.7 alpha:1.0];

    label.text = placeholder;
    label.alpha = 1;
    label.tag = 999;
    [tView addSubview:label];
    [label sizeToFit];
    if(![tView.text isEqualToString:@""])
        [label setAlpha:0];
    [label release];
}

Można za pomocą tego zarządzać tekstem zastępczym..

- (void)textViewDidChange:(UITextView *)textView
{
    [self textChange:textView];
}

- (void)textChange:(UITextView *)textView
{
    if([textView.text length]>0)
        [[textView viewWithTag:999] setAlpha:0];
    else
        [[textView viewWithTag:999] setAlpha:1];
}
 1
Author: sinh99,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-12 05:19:04

Innym rozwiązaniem jest umieszczenie elementu zastępczego UITextView tuż nad rzeczywistym UITextView. Daj tę samą ramkę. Ustaw placeholderTextView.enabled = NO dla widoku tekstu zastępczego.

Następnie Ustaw delegata tylko dla zwykłego textView i zdefiniuj tę metodę UITextViewDelegate:

- (void)textViewDidChange:(UITextView *)textView
{
    NSLog(@"textViewDidChange");

    if(self.textView.text.length == 0)
    {
        self.placeholderTextView.hidden = NO;
    }
    else
    {
        self.placeholderTextView.hidden = YES;
    }
}
 0
Author: ill_always_be_a_warriors,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-11-09 14:13:37