先说说记录这个的原因吧,关于限制UITextField的输入字数的各种需求,已经疲软了,工作几年中用的还真不少,有只要字数长度的,也有字符长度的,当然这个就包括中文和英文,还有.....标点符号也分中文和英文的,每次都多不情愿的写这个地方,闲来无事,就来记录一下,方便大家,若有不对的地方,多多指教哈。关于UITextField大家肯定对下面这个方法非常熟悉- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string那么这个函数的大概意思是什么呢?我是这么理解的(个人认为)由于返回的是个 BOOL 所以我猜想大概就是我们在输入的时候,是否对UITextField 的内容进行修改下面来简单分析下参数textField 这个参数大家都懂,就是响应该协议的UITextField对象range在这个函数中,添加了打印信息,测试了下- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{ NSLog(@" 打印信息:%lu -----%lu ",(unsigned long)range.location,(unsigned long)range.length);}//打印log2017-01-25 17:48:49.826 TestObject[5502:683375] 打印信息toBeString:Zxtgfd2017-01-25 17:48:50.506 TestObject[5502:683375] 打印信息:6 -----0 2017-01-25 17:48:50.508 TestObject[5502:683375] 打印信息toBeString:Zxtgfda2017-01-25 17:48:51.894 TestObject[5502:683375] 打印信息:6 -----1由此可以看到,在输入的时候,range.length为0,range.location为插入时的位置 ,range.length为1时,是我在点击删除的时候打印的,应该可以表示删除(不确定)string string.length为0时,表示删除。限制输入长度1 如果只是输入英文状态,或者数字状态,那么我们我们可以利用这个函数,实现方法为:- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{ NSLog(@" 打印信息:%lu -----%lu ",(unsigned long)range.location,(unsigned long)range.length); if (textField.text.length + string.length > kMaxNumber) { return NO; } //删除按钮 if (string.length == 0) { return YES; } return YES;}2 但是,如果要涉及到中文的话,这样就不行了。因为,在输入中文的时候,如果你还没有确定所输入的中文,那么UITextField此时接收的并不是中文,而是对应的拼音,比如你想输入汉字君不见黄河之水天上来 那么对应的拼音则是junbujianhuanghezhishuitianshanglai这么一来,比如你想限制的输入个数为10 那么只能输入到junbujianh这个位置。那么我们该怎么解决问题呢,进入API我们可以发现。UITextField : UIControl ---- UITextField继承自UIControl有如下函数- (void)addTarget:(nullable id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;typedef NS_OPTIONS(NSUInteger, UIControlEvents) { UIControlEventTouchDown = 1 << 0, // on all touch downs UIControlEventTouchDownRepeat = 1 << 1, // on multiple touchdowns (tap count > 1) UIControlEventTouchDragInside = 1 << 2, UIControlEventTouchDragOutside = 1 << 3, UIControlEventTouchDragEnter = 1 << 4, UIControlEventTouchDragExit = 1 << 5, UIControlEventTouchUpInside = 1 << 6, UIControlEventTouchUpOutside = 1 << 7, UIControlEventTouchCancel = 1 << 8, UIControlEventValueChanged = 1 << 12, // sliders, etc. UIControlEventPrimaryActionTriggered NS_ENUM_AVAILABLE_IOS(9_0) = 1 << 13, // semantic action: for buttons, etc. UIControlEventEditingDidBegin = 1 << 16, // UITextField UIControlEventEditingChanged = 1 << 17, UIControlEventEditingDidEnd = 1 << 18, UIControlEventEditingDidEndOnExit = 1 << 19, // 'return key' ending editing UIControlEventAllTouchEvents = 0x00000FFF, // for touch events UIControlEventAllEditingEvents = 0x000F0000, // for UITextField UIControlEventApplicationReserved = 0x0F000000, // range available for application use UIControlEventSystemReserved = 0xF0000000, // range reserved for internal framework use UIControlEventAllEvents = 0xFFFFFFFF};我们可以利用此函数和对应的枚举UIControlEventEditingChanged来监测输入的内容变化,从而控制输入的字数代码如下- (void)textFiledDidChange:(UITextField *)textField{ if (kMaxNumber == 0) return; NSString *toBeString = textField.text; NSLog(@" 打印信息toBeString:%@",toBeString); NSString *lang = [[textField textInputMode] primaryLanguage]; // 键盘输入模式 if ([lang isEqualToString:@"zh-Hans"]) { // 简体中文输入,包括简体拼音,健体五笔,简体手写 //判断markedTextRange是不是为Nil,如果为Nil的话就说明你现在没有未选中的字符, //可以计算文字长度。否则此时计算出来的字符长度可能不正确 UITextRange *selectedRange = [textField markedTextRange]; //获取高亮部分(感觉输入中文的时候才有) UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0]; // 没有高亮选择的字,则对已输入的文字进行字数统计和限制 if (!position) { //中文和字符一起检测 中文是两个字符 if ([toBeString getStringLenthOfBytes] > kMaxNumber) { textField.text = [toBeString subBytesOfstringToIndex:kMaxNumber]; } } } else { if ([toBeString getStringLenthOfBytes] > kMaxNumber) { textField.text = [toBeString subBytesOfstringToIndex:kMaxNumber]; } }}其中有正则表达式的验证,方法我是写在NSString的扩展中的,具体如下//.h文件#import@interface NSString (category)
- (NSInteger)getStringLenthOfBytes;
- (NSString *)subBytesOfstringToIndex:(NSInteger)index;
@end
//.m文件
#import "NSString+category.h"
@implementation NSString (category)
- (NSInteger)getStringLenthOfBytes
{
NSInteger length = 0;
for (int i = 0; i<[self length]; i++) {
//截取字符串中的每一个字符
NSString *s = [self substringWithRange:NSMakeRange(i, 1)];
if ([self validateChineseChar:s]) {
NSLog(@" s 打印信息:%@",s);
length +=2;
}else{
length +=1;
}
NSLog(@" 打印信息:%@ %ld",s,(long)length);
}
return length;
}
- (NSString *)subBytesOfstringToIndex:(NSInteger)index
{
NSInteger length = 0;
NSInteger chineseNum = 0;
NSInteger zifuNum = 0;
for (int i = 0; i<[self length]; i++) {
//截取字符串中的每一个字符
NSString *s = [self substringWithRange:NSMakeRange(i, 1)];
if ([self validateChineseChar:s])
{
if (length + 2 > index)
{
return [self substringToIndex:chineseNum + zifuNum];
}
length +=2;
chineseNum +=1;
}
else
{
if (length +1 >index)
{
return [self substringToIndex:chineseNum + zifuNum];
}
length+=1;
zifuNum +=1;
}
}
return [self substringToIndex:index];
}
//检测中文或者中文符号
- (BOOL)validateChineseChar:(NSString *)string
{
NSString *nameRegEx = @"[\\u0391-\\uFFE5]";
if (![string isMatchesRegularExp:nameRegEx]) {
return NO;
}
return YES;
}
//检测中文
- (BOOL)validateChinese:(NSString*)string
{
NSString *nameRegEx = @"[\u4e00-\u9fa5]";
if (![string isMatchesRegularExp:nameRegEx]) {
return NO;
}
return YES;
}
- (BOOL)isMatchesRegularExp:(NSString *)regex {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
return [predicate evaluateWithObject:self];
}
@end
网友评论