iOS技术点iOS 移动端开发面试

iOS UITextField输入限制(中英文混合)

2017-02-04  本文已影响1739人  半笑半醉間

先说说记录这个的原因吧,关于限制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);
}

//打印log
2017-01-25 17:48:49.826 TestObject[5502:683375]  打印信息toBeString:Zxtgfd
2017-01-25 17:48:50.506 TestObject[5502:683375]  打印信息:6  -----0 
2017-01-25 17:48:50.508 TestObject[5502:683375]  打印信息toBeString:Zxtgfda
2017-01-25 17:48:51.894 TestObject[5502:683375]  打印信息:6  -----1 

由此可以看到,在输入的时候,range.length为0,range.location为插入时的位置 ,range.length为1时,是我在点击删除的时候打印的,应该可以表示删除(不确定)
string string.length为0时,表示删除。

限制输入长度
- (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;
}
- (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 <Foundation/Foundation.h>

@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

当然,如果遇到UITextView限制字数的时候,由于不是继承1所以没有addtarget方法,但是可以通过添加通知UITextViewTextDidChangeNotification的方法来监测 ,方法还是和上面一样。

上一篇下一篇

猜你喜欢

热点阅读