如何在UITextView中插入占位符?

有没有办法在UITextView插入一个类似于UITextField的占位符? 如果是的话,请给我任何链接或任何想法来实现这个function。

在UITextView中创build占位符是不可能的,但是你可以通过这个来产生像占位符一样的效果。

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

或者你可以在textview中添加标签

  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 *)theTextView { if (![textView hasText]) { lbl.hidden = NO; } } - (void) textViewDidChange:(UITextView *)textView { if(![textView hasText]) { lbl.hidden = NO; } else{ lbl.hidden = YES; } } 

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

如果这是你的事情,你甚至可以添加小animation来淡入/淡出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; }]; } 

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

您可以使用下面的function作为占位符,使用您可以在其上设置占位符。

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

你可以使用这个pipe理占位符文本

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

另一个解决scheme是把一个占位符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; } }