您的位置:首页 > 运维架构

关于textView实时监控输入文字长度

2016-03-14 19:10 417 查看
之前在做实时监控textView的文字长度,控制输入文字的多少这个效果时,找不到方法,最后查看资料,找到了解决办法,分享给大家一下。

//设置输入框并且标明文字
UIView *inputView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(effect.frame) + 10, SCREENW, 150)];
self.inputView = inputView;
inputView.backgroundColor = [UIColor whiteColor];
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(5, 5, SCREENW - 10, inputView.height - 30)];
textView.backgroundColor = [UIColor clearColor];
[inputView addSubview:textView];
self.textView = textView;
textView.font = [UIFont systemFontOfSize:12];
textView.delegate = self;
textView.scrollEnabled = NO;
textView.textColor = RGBA(37, 48, 59, 0.9);
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineSpacing = 6;// 字体的行间距
NSDictionary *attributes = @{
NSForegroundColorAttributeName:RGBA(37, 48, 59, 0.4),
NSFontAttributeName:[UIFont systemFontOfSize:12],
NSParagraphStyleAttributeName:paragraphStyle
};
textView.attributedText = [[NSAttributedString alloc] initWithString:@"环境怎么样,服务是否周到?亲,快些下您的评论吧!(15字以上)" attributes:attributes];

UILabel *textLable = [[UILabel alloc] initWithFrame:CGRectMake(textView.x, inputView.height - 20, textView.width, 20)];
self.textLable = textLable;
textLable.text = @"还可以输入120字";
textLable.textColor = RGBA(37, 48, 59, 0.4);
textLable.font = [UIFont systemFontOfSize:12];
textLable.textAlignment = NSTextAlignmentRight;
[inputView addSubview:textLable];
[self.view addSubview:inputView];


然后实时监控更改提示文字

if (textView.text.length < 120) {
self.textLable.text = [NSString stringWithFormat:@"还可以输入%zd字",120 - textView.text.length];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  IOS