UITextView 解决字数限制问题和placehorder问题
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UITextView 解决字数限制问题和placehorder问题相关的知识,希望对你有一定的参考价值。
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问题的主要内容,如果未能解决你的问题,请参考以下文章