项目开发中经常用到类似这样的功能,文本输入的时候需要动态的显示文本输入字数。这里以多行文本框为例,介绍一下实现方案。
核心代码如下
-(void)textViewDidChange:(UITextView *)textView
{
if (textView.text.length>150)
{
textView.text=[textView.text substringFromIndex:textView.text.length-150];
//超出150个字数会有弹框提醒
[CCCommonAPI showCommonAlertTitle:K_CC_LOCAL_STR(@"Alert.prompt") message:K_CC_LOCAL_STR(@"hignseaspool.enter.characters") cancelAction:NO otherActionTitle:K_CC_LOCAL_STR(@"Alert.confirm") otherActionHandler:nil persentCompletion:nil];
}
self.lblTip.text = [NSString stringWithFormat:@"%ld/150",self.textReason.text.length];
}