如何在UItextView中添加placeHolder文本?在iPhone SDK的

问题描述:

可能重复:
Placeholder in UITextView如何在UItextView中添加placeHolder文本?在iPhone SDK的

在iPhone应用程序如何在UItextView添加占位符文本(持有一些默认的文本)?

+0

请你所说的“占位”什么 – 2011-01-29 06:58:07

+0

这将解释编辑您的问题帮助你:http://*.com/a/17451491/1442541 – evya 2013-07-03 15:04:27

简单,我就是这么做的..伟大的工作,对我来说。希望这有助于有人..

#pragma mark - 
#pragma mark TextView Delegate methods 


    UITextView itsTextView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, itsTextView.frame.size.width, itsTextView.frame.size.height)]; 
      [itsTextView setDelegate:self]; 
      [itsTextView setReturnKeyType:UIReturnKeyDone]; 
      [itsTextView setText:@"List words or terms separated by commas"]; 
      [itsTextView setFont:[UIFont fontWithName:@"HelveticaNeue" size:11]]; 
      [itsTextView setTextColor:[UIColor lightGrayColor]]; 

- (BOOL) textViewShouldBeginEditing:(UITextView *)textView 
{ 
    if (itsTextView.textColor == [UIColor lightGrayColor]) { 
     itsTextView.text = @""; 
     itsTextView.textColor = [UIColor blackColor]; 
    } 

    return YES; 
} 

-(void) textViewDidChange:(UITextView *)textView 
{ 
    if(itsTextView.text.length == 0){ 
     itsTextView.textColor = [UIColor lightGrayColor]; 
     itsTextView.text = @"List words or terms separated by commas"; 
     [itsTextView resignFirstResponder]; 
    } 
} 

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { 

    if([text isEqualToString:@"\n"]) { 
     [textView resignFirstResponder]; 
     if(itsTextView.text.length == 0){ 
      itsTextView.textColor = [UIColor lightGrayColor]; 
      itsTextView.text = @"List words or terms separated by commas"; 
      [itsTextView resignFirstResponder]; 
     } 
     return NO; 
    } 

    return YES; 
}