【问题标题】:How to add placeHolder Text in UItextView? in iphone sdk [duplicate]如何在 UItextView 中添加 placeHolder 文本?在 iphone sdk [重复]
【发布时间】:2011-06-17 15:50:45
【问题描述】:

可能重复:
Placeholder in UITextView

在 iPhone 应用程序中如何在UItextView 中添加 placeHolder 文本(以保存一些默认文本)?

【问题讨论】:

标签: iphone objective-c xcode ios4 uitextview


【解决方案1】:

很简单,我就是这样做的。对我来说效果很好。希望这对某人有所帮助。。

#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;
}

【讨论】:

  • 我包含了上面的代码,但是在开始编辑时它会清除占位符,但在 viewdidchange 中它总是放置占位符,因此用户无法在 textview 中编辑/键入是否有任何解决方案
  • Hai @sugan.s 我猜你没有将文本颜色设置为 '[itsTextView setTextColor:[UIColor lightGrayColor]];'
  • 如您所见,我们正在根据文本颜色清除文本..如果有帮助请告诉我..
  • 我应该在这里包含代码吗?你能帮我吗
  • 海素干,请把代码发给dilip.id@me.com 我会尽力提供帮助,或者你可以将它作为新问题发布并在此处提供链接..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-03
相关资源
最近更新 更多