如何在UITextView中插入占位符?

发布于 2024-11-29 09:26:29 字数 100 浏览 0 评论 0原文

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

Is there any way to insert a placeholder in a UITextView similar to the UITextField? If yes, then please send me any link or any idea to implement this functionality.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

作妖 2024-12-06 09:26:29

无法在 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;
    }  
}

It is not possible to create placeholder in UITextView but you can generate effect like place holder by this.

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

OR you can add label in textview just like

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;
    }  
}
七堇年 2024-12-06 09:26:29

另一个简单的解决方案是只需将 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;
    }];
}

Another easy solution is to just add a UILabel to your UITextView subview.

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

You can even add little animations to fade in/out the UILabel if that's your thing.

- (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;
    }];
}
累赘 2024-12-06 09:26:29

另一个简单的解决方案是为 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;
    }
}

Another easy solution is set YES/NO for hidden properties of 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;
    }
}
那小子欠揍 2024-12-06 09:26:29
-(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;
}
-(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;
}
¢好甜 2024-12-06 09:26:29

您可以使用下面的函数来制作占位符,使用它您可以在其上设置占位符。

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

You can use as below function for making placeholder, Using thi you can set place holder on it..

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

You can manage placeholder text using this..

- (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];
}
べ映画 2024-12-06 09:26:29

另一个解决方案是将占位符 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;
    }
}

Another solution is to put a placeholder UITextView right on top of the actual UITextView. Give it the same frame. Set placeholderTextView.enabled = NO for the placeholder TextView.

Then, set the delegate for the normal textView only and define this UITextViewDelegate method:

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

    if(self.textView.text.length == 0)
    {
        self.placeholderTextView.hidden = NO;
    }
    else
    {
        self.placeholderTextView.hidden = YES;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文