如何在UITextView中插入占位符? [英] How to insert placeholder in UITextView?

查看:117
本文介绍了如何在UITextView中插入占位符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在 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.

推荐答案

无法创建占位符UITextView,但你可以通过这个生成像占位符这样的效果。

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

或者你可以在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 *) textView
{
     if (![textView hasText]) {
     lbl.hidden = NO;
}
}

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

这篇关于如何在UITextView中插入占位符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆