【问题标题】:How to insert placeholder in UITextView? [duplicate]如何在 UITextView 中插入占位符? [复制]
【发布时间】:2011-10-25 17:11:15
【问题描述】:

有没有办法在UITextView 中插入一个占位符,类似于UITextField?如果是,请向我发送任何链接或任何想法来实现此功能。

【问题讨论】:

标签: iphone uitextview


【解决方案1】:

在 UITextView 中无法创建占位符,但可以通过此生成占位符的效果。

- (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];
    }
}

或者你可以像在文本视图中添加标签一样

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];

并设置

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

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

【讨论】:

  • 它很好很简单,但 textViewShouldBeginEditing 总是会删除所有文本 - 你可能不希望当用户输入一些文本时,离开文本视图,然后再次返回编辑...... 。可以只添加一个颜色检查 + 只在它是灰色时擦除。
  • 如果我有多个 UITextView 怎么办?
  • @Odelya 我没有遇到这个问题,但我认为你可以通过将标签传递给他们并根据标签条件更改显示隐藏标签来做到这一点。比如 if (txtv​​iew.tag == 1 ) {你的代码}。
  • 永远不要忘记将 textview 委托方法设置为 self,在 ViewDidLoad 中插入这一行 commentTxtView.delegate=self;
  • 不错的解决方案我改变了一点方法 - (BOOL) textViewShouldBeginEditing:(UITextView *)textView { if ([commentTxtView.text isEqualToString:@"Comment"]) { commentTxtView.text = @ ""; commentTxtView.textColor = [UIColor blackColor]; } 返回是;当 TextView 再次获得焦点时,它会有所帮助。
【解决方案2】:

另一个简单的解决方案是将UILabel 添加到您的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];
  }
}

如果您愿意,您甚至可以添加小动画来淡入/淡出 UILabel

- (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;
    }];
}

【讨论】:

  • 这可能行得通,但它违反了 Apple 的明确建议:“虽然从技术上讲可以将子视图添加到标准系统控件(从 UIControl 继承的对象)中,但您永远不应该以这种方式自定义它们。 "这是来自标题为“不要通过嵌入子视图自定义控件”部分中的developer.apple.com/library/ios/#documentation/WindowsViews/…
  • 当然,我同意。但是在苹果实现这个功能之前,这是一个相当不错的解决方案。
  • @algal 那么,你有什么建议?覆盖drawRect: 不是同样(或者可能更)危险吗?
  • @MattDiPasquale 所选答案中的两个选项中的第一个似乎很干净,因为它仅向视图控制器添加逻辑并且仅通过其预期接口修改 UITextView。
  • @algal 很好,但该解决方案并不完美: 1. 它会删除 textViewShouldBeginEditing: 而不是 !textField.hasText() 上的占位符文本。 2.它不是模块化的。就像,我宁愿有一个名为PlaceholderTextView 的自定义子类(例如,观察UITextViewTextDidChangeNotification),我可以直接插入并将其设置为placeholder 属性。
【解决方案3】:

另一个简单的解决方案是为 placeholderLabel 的隐藏属性设置 YES/NO

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

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

【讨论】:

    【解决方案4】:
    -(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;
    }
    

    【讨论】:

      【解决方案5】:

      您可以使用以下功能来制作占位符,使用 thi 您可以在其上设置占位符..

      [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];
      }
      

      您可以使用此管理占位符文本..

      - (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];
      }
      

      【讨论】:

        【解决方案6】:

        另一种解决方案是将占位符 UITextView 放在实际 UITextView 的顶部。给它相同的框架。为占位符 TextView 设置placeholderTextView.enabled = NO

        然后,只为普通的 textView 设置委托,并定义这个 UITextViewDelegate 方法:

        - (void)textViewDidChange:(UITextView *)textView
        {
            NSLog(@"textViewDidChange");
        
            if(self.textView.text.length == 0)
            {
                self.placeholderTextView.hidden = NO;
            }
            else
            {
                self.placeholderTextView.hidden = YES;
            }
        }
        

        【讨论】:

          猜你喜欢
          • 2012-09-28
          • 2010-11-22
          • 2011-07-17
          • 2016-06-24
          • 1970-01-01
          • 2016-06-21
          • 2015-06-28
          • 2021-11-18
          • 1970-01-01
          相关资源
          最近更新 更多