2016-01-07 textview 字数限制
2016-01-07 13:18
274 查看
字母数字 限制字数 delegate
//控制输入文字的长度和内容,可通调用以下代理方法实现
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if (range.location>=20)
{
//控制输入文本的长度
UIAlertView *alve = [[UIAlertView alloc]initWithTitle:@"提示" message:@"输入内容最多20字" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alve show];
return NO;
}
if ([text isEqualToString:@"\n"]) {
//禁止输入换行
[textView resignFirstResponder];
return NO;
}
else
{
return YES;
}
}
3.汉字的
#define MaxNumberOfDescriptionChars 20
viewdidload 中
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewEditChanged:) name:UITextViewTextDidChangeNotification object:_descrViewa];
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[[NSNotificationCenter defaultCenter]removeObserver:self
name:@"UITextViewTextDidChangeNotification"
object:_descrViewa];
}
// 监听文本改变
-(void)textViewEditChanged:(NSNotification *)obj{
UITextView *textView = (UITextView *)obj.object;
NSString *toBeString = textView.text;
NSString *lang = [[UITextInputMode currentInputMode] primaryLanguage]; // 键盘输入模式
if ([lang isEqualToString:@"zh-Hans"]) { // 简体中文输入,包括简体拼音,健体五笔,简体手写
UITextRange *selectedRange = [textView markedTextRange];
//获取高亮部分
UITextPosition *position = [textView positionFromPosition:selectedRange.start offset:0];
// 没有高亮选择的字,则对已输入的文字进行字数统计和限制
if (!position) {
if (toBeString.length > MaxNumberOfDescriptionChars) {
textView.text = [toBeString substringToIndex:MaxNumberOfDescriptionChars];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"不能超过20个字!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
}
}
// 有高亮选择的字符串,则暂不对文字进行统计和限制
else{
}
}
// 中文输入法以外的直接对其统计限制即可,不考虑其他语种情况
else{
if (toBeString.length > MaxNumberOfDescriptionChars) {
textView.text = [toBeString substringToIndex:MaxNumberOfDescriptionChars];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"不能超过20个字!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
}
}
}
//控制输入文字的长度和内容,可通调用以下代理方法实现
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if (range.location>=20)
{
//控制输入文本的长度
UIAlertView *alve = [[UIAlertView alloc]initWithTitle:@"提示" message:@"输入内容最多20字" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alve show];
return NO;
}
if ([text isEqualToString:@"\n"]) {
//禁止输入换行
[textView resignFirstResponder];
return NO;
}
else
{
return YES;
}
}
3.汉字的
#define MaxNumberOfDescriptionChars 20
viewdidload 中
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewEditChanged:) name:UITextViewTextDidChangeNotification object:_descrViewa];
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[[NSNotificationCenter defaultCenter]removeObserver:self
name:@"UITextViewTextDidChangeNotification"
object:_descrViewa];
}
// 监听文本改变
-(void)textViewEditChanged:(NSNotification *)obj{
UITextView *textView = (UITextView *)obj.object;
NSString *toBeString = textView.text;
NSString *lang = [[UITextInputMode currentInputMode] primaryLanguage]; // 键盘输入模式
if ([lang isEqualToString:@"zh-Hans"]) { // 简体中文输入,包括简体拼音,健体五笔,简体手写
UITextRange *selectedRange = [textView markedTextRange];
//获取高亮部分
UITextPosition *position = [textView positionFromPosition:selectedRange.start offset:0];
// 没有高亮选择的字,则对已输入的文字进行字数统计和限制
if (!position) {
if (toBeString.length > MaxNumberOfDescriptionChars) {
textView.text = [toBeString substringToIndex:MaxNumberOfDescriptionChars];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"不能超过20个字!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
}
}
// 有高亮选择的字符串,则暂不对文字进行统计和限制
else{
}
}
// 中文输入法以外的直接对其统计限制即可,不考虑其他语种情况
else{
if (toBeString.length > MaxNumberOfDescriptionChars) {
textView.text = [toBeString substringToIndex:MaxNumberOfDescriptionChars];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"不能超过20个字!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
}
}
}
相关文章推荐
- 2016/01/07 股票晚上应该做的准备工作
- 写在2016年
- java 后台走过的坑
- python小点心--set
- Eclipse设置背景色
- 关于文件查找遍历,windows下的使用
- linux下screen工具使用
- hibernate的缓存机制
- 【LeetCode】283. Move Zeroes
- vs 打印信息到Output
- java怎么连接sql server,需要注意的几点
- Javascript面向对象编程(一):封装
- maven使用实例记录
- Python Web 实现Ajax调用
- Objective-C点语法
- HTML <fieldset> 标签的使用
- IOS object-c 中定时器NSTimer的开启与关闭
- POJ 1426-Find The Multiple
- wampserver部署笔记 设置apache使得外网可以访问
- Android 第三方开源 SlidingMenu 使用详解