TextView里限制输入字数的方法
2015-05-03 13:32
411 查看
一开始采用的方法是函数textView:shouldChangeTextInRange:replacementText:来进行判断:
//键入Done时,插入换行符,然后执行addBookmark
- (BOOL)textView:(UITextView *)textView
shouldChangeTextInRange:(NSRange)range
replacementText:(NSString *)text
{
//判断加上输入的字符,是否超过界限
NSString *str = [NSString stringWithFormat:@"%@%@", textView.text, text];
if (str.length > BOOKMARK_WORD_LIMIT)
{
textView.text = [textView.text substringToIndex:BOOKMARK_WORD_LIMIT];
return NO;
}
return YES;
}
但在使用中发现该方法在有联想输入的时候,根本无法对联想输入的词进行判断,然后尝试使用textViewDidChange:,验证可行:
#define MaxNumberOfDescriptionChars 100//textview最多输入的字数
/*由于联想输入的时候,函数textView:shouldChangeTextInRange:replacementText:无法判断字数,
因此使用textViewDidChange对TextView里面的字数进行判断
*/
- (void)textViewDidChange:(UITextView *)textView
{
//该判断用于联想输入
if (textView.text.length > MaxNumberOfDescriptionChars)
{
textView.text = [textView.text substringToIndex:MaxNumberOfDescriptionChars];
}
//还可输入的字数
self.countLabel.text=[NSString stringWithFormat:@"%lu",MaxNumberOfDescriptionChars-textView.text.length];
}
//键入Done时,插入换行符,然后执行addBookmark
- (BOOL)textView:(UITextView *)textView
shouldChangeTextInRange:(NSRange)range
replacementText:(NSString *)text
{
//判断加上输入的字符,是否超过界限
NSString *str = [NSString stringWithFormat:@"%@%@", textView.text, text];
if (str.length > BOOKMARK_WORD_LIMIT)
{
textView.text = [textView.text substringToIndex:BOOKMARK_WORD_LIMIT];
return NO;
}
return YES;
}
但在使用中发现该方法在有联想输入的时候,根本无法对联想输入的词进行判断,然后尝试使用textViewDidChange:,验证可行:
#define MaxNumberOfDescriptionChars 100//textview最多输入的字数
/*由于联想输入的时候,函数textView:shouldChangeTextInRange:replacementText:无法判断字数,
因此使用textViewDidChange对TextView里面的字数进行判断
*/
- (void)textViewDidChange:(UITextView *)textView
{
//该判断用于联想输入
if (textView.text.length > MaxNumberOfDescriptionChars)
{
textView.text = [textView.text substringToIndex:MaxNumberOfDescriptionChars];
}
//还可输入的字数
self.countLabel.text=[NSString stringWithFormat:@"%lu",MaxNumberOfDescriptionChars-textView.text.length];
}
相关文章推荐
- TextView里限制输入字数的方法
- 【IOS】TextView里限制输入字数的方法
- TextView里限制输入字数的方法
- TextView里限制输入字数的方法
- android EditText限制输入字数的方法
- Android EditText限制输入字数的方法
- textView限制字数(超简单,不走弯路)(解决联想输入及iOS7崩溃等问题)
- iOS【textView或者textField文本输入字数限制】
- 关于iOS对textView字数限制上的一些坑以及解决方法
- textView控制输入字数的限制
- 【iOS】TextView里限制输入法字数的方法
- Android 限制textview Edittext 只能输入电话号码和特定字符的方法
- android设置textview限制字数以省略号显示的方法
- TextView如何限制输入字数
- iOS 怎么限制textView和textField的输入字数
- iOS TextView限制输入字数且屏蔽Emoji表情符号
- ios开发之--实现TextView提示文字并且输入字数限制
- iOS TextView限制输入字数且屏蔽Emoji表情符号
- Android EditText输入字数限制总结(包括中文输入内存溢出的解决方法)
- textfield/textview的子类,实现功能:几种不同类型的最大输入字符串字数限制,剩余可输入字符数,emoji表情过滤