UITextField只允許輸入中,英文,數字

fpkoko發表於2017-12-13

某些情況下要UITextField只能輸入某一種特定的字元.比如大寫A-Z或者小寫a-z,或者漢字.或者數字. 這樣的話使用NSPredicate正規表示式可以過濾 首先設定UItextField的代理 實現如下方法:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    if ([self isInputRuleAndNumber:string] || [string isEqualToString:@""]) {
        return YES;
    }
    return NO;
}
複製程式碼

然後新增事件,因為選擇輸入法聯想的詞.是不會經過textField:shouldChangeCharactersInRange:replacementString:方法的

    [textField addTarget:self action:@selector(textFieldChanged:) forControlEvents:UIControlEventEditingChanged];

複製程式碼

實現:

- (void)textFieldChanged:(UITextField *)textField {
    
    NSString *toBeString = textField.text;
    NSString *lastString;
    if(toBeString.length>0)
        lastString=[toBeString substringFromIndex:toBeString.length-1];

    if (![self isInputRuleAndNumber:toBeString]&&[self hasEmoji:lastString]) {
        textField.text = [self disable_emoji:toBeString];
        return;
    }
    NSString *lang = [[textField textInputMode] primaryLanguage];
    if([lang isEqualToString:@"zh-Hans"]) {
        UITextRange *selectedRange = [textField markedTextRange];
        UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
        if(!position) {
            NSString *getStr = [self getSubString:toBeString];
            if(getStr && getStr.length > 0) {
                textField.text = getStr;
            }
        }
    } else{
        NSString *getStr = [self getSubString:toBeString];
        if(getStr && getStr.length > 0) {
            textField.text= getStr;
        }
    }
}
複製程式碼

再來實現限制:

pattern中,輸入需要驗證的通過的字元 小寫a-z 大寫A-Z 漢字\u4E00-\u9FA5 數字\u0030-\u0039


- (BOOL)isInputRuleAndNumber:(NSString *)str {
    NSString *pattern = @"[a-zA-Z\u4E00-\u9FA5\\u0030-\\u0039]";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
    BOOL isMatch = [pred evaluateWithObject:str];
    return isMatch;
}
複製程式碼

實現判斷是否是Emoji

- (BOOL)hasEmoji:(NSString*)str{
    NSString *pattern = @"[^\\u0020-\\u007E\\u00A0-\\u00BE\\u2E80-\\uA4CF\\uF900-\\uFAFF\\uFE30-\\uFE4F\\uFF00-\\uFFEF\\u0080-\\u009F\\u2000-\\u201f\r\n]";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
    BOOL isMatch = [pred evaluateWithObject:str];
    return isMatch;
}
複製程式碼

字元限制


#define kMaxLength 20;
-(NSString *)getSubString:(NSString*)string
{
    NSStringEncoding encoding = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
    NSData* data = [string dataUsingEncoding:encoding];
    NSInteger length = [data length];
    if (length > kMaxLength) {
        NSData *data1 = [data subdataWithRange:NSMakeRange(0, kMaxLength)];
        NSString *content = [[NSString alloc] initWithData:data1 encoding:encoding];
        if (!content || content.length == 0) {
            data1 = [data subdataWithRange:NSMakeRange(0, kMaxLength - 1)];
            content =  [[NSString alloc] initWithData:data1 encoding:encoding];
        }
        return content;
    }
    return nil;
}
複製程式碼
參考資料:30分鐘入門正則教程
https://share.weiyun.com/cecfd1aa61226d176875266801025b36
複製程式碼

相關文章