码迷,mamicode.com
首页 > 其他好文 > 详细

UITextView 解决字数限制问题和placehorder问题

时间:2016-01-25 19:05:37      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:

UITextView没有placehorder这个属性,所以我们要在textView上加个label,充当placehorder。下面代码会一一展现

- (void)createUI{

    UITextView * textView = [[UITextView alloc] initWithFrame:CGRectMake(52, 50, SCREEN_WIDTH - 50, 50)];

    textView.backgroundColor=[UIColor orangeColor]; //背景色

    textView.editable = YES;        //是否允许编辑内容,默认为“YES”

    textView.delegate = self;       //设置代理方法的实现类

    //        textView.font=[UIFont fontWithName:@"Arial" size:13]; //设置字体名字和字体大小;

    textView.font = [UIFont systemFontOfSize:13];

    textView.returnKeyType = UIReturnKeyDefault;//return键的类型

    //            textView.keyboardType = UIKeyboardTypeDefault;//键盘类型

    textView.spellCheckingType = UITextSpellCheckingTypeYes;

    textView.textAlignment = NSTextAlignmentLeft; //文本显示的位置默认为居左

    textView.textColor = [UIColor blackColor];

    [self.view addSubview:textView];

    

    

    UILabel * placehorder = [[UILabel alloc] initWithFrame:CGRectMake(59, 4, SCREEN_WIDTH - 50, 40)];

    placehorder.textColor = [UIColor lightGrayColor];

    placehorder.text = @"placehorder";

    placehorder.font = [UIFont systemFontOfSize:13];

    placehorder.tag = 1;

    [textView addSubview:placehorder];

}

 

#pragma mark - UITextViewDelegate

 

- (void)textViewDidBeginEditing:(UITextView *)textView{  

    UILabel *placehorder = (UILabel *)[self.view viewWithTag:1];

        if (textView.text.length > 0) {

            placehorder.hidden = YES;

        }else{

            placehorder.hidden = NO;

    }

}

- (void)textViewDidChange:(UITextView *)textView{

    UILabel *placehorder = (UILabel *)[self.view viewWithTag:1];

    if (placehorder.tag == 1) {

        if (textView.text.length > 0) {

            placehorder.hidden = YES;

        }else{

            placehorder.hidden = NO;

        }

    }

    if (textView.markedTextRange==nil && textView.text.length > 45) {

        

        textView.text=[textView.text substringToIndex:45];

    } 

}

 

UITextView 解决字数限制问题和placehorder问题

标签:

原文地址:http://www.cnblogs.com/lizhen0203/p/5158049.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!