UITextView设置类似placeholder的效果

来源:互联网 发布:汇成凯网络 编辑:程序博客网 时间:2024/05/22 16:53

想要实现类似UITextFiled的placeholder的效果有如下两种方式,代码实现方式如下:

一、方法一:

- (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中设置相关的UILable

UILabel *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;    }  }


如此简单,就是这么任性,亲,你会了吗???


1 0
原创粉丝点击