UITextField

如何更好地限制一個(gè)UITextField的輸入長(zhǎng)度http://www.itdecent.cn/p/7a69f15c0268

/**
 *  關(guān)閉鍵盤
 */
- (void)closeKeyboard;

#pragma mark - 關(guān)閉鍵盤
- (void)closeKeyboard {
    //添加手勢(shì)關(guān)閉鍵盤
    UITapGestureRecognizer * gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeKeyboard:)];
    gesture.cancelsTouchesInView = NO;
    [self.view addGestureRecognizer:gesture];
}
- (void)closeKeyboard:(UITapGestureRecognizer*)sender {
    [self.view endEditing:YES];
}
[_phoneText addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [_phoneText addTarget:self action:@selector(textFieldEditingDidEnd:) forControlEvents:UIControlEventEditingDidEnd];

#pragma mark - UITextFieldDelegate 輸入只能是數(shù)字

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    return [ToolsClassManager validateNumber:string];
}

#pragma mark - UIControlEventEditingChanged 限制長(zhǎng)度

-(void)textFieldDidChange:(UITextField *)textField{
    if (textField == self.phoneText) {
        if (textField.text.length > 11) {
            textField.text = [textField.text substringToIndex:11];
        }
    }
}

#pragma mark - UIControlEventEditingDidEnd 正則判斷手機(jī)號(hào)碼地址格式

- (void)textFieldEditingDidEnd:(UITextField *)textField {
    if (![ToolsClassManager isMobileNumber:textField.text]) {
        [ZCLUtilities showMessageHud:@"請(qǐng)輸入正確的手機(jī)號(hào)" duration:1.0];
    }
}
@implementation ToolsClassManager
/**
 *  輸入只能是數(shù)字
 */
+ (BOOL)validateNumber:(NSString*)number;

/**
 *  正則判斷手機(jī)號(hào)碼地址格式
 */
+ (BOOL)isMobileNumber:(NSString *)mobileNum;
#pragma mark - 正則判斷手機(jī)號(hào)碼地址格式
+ (BOOL)isMobileNumber:(NSString *)mobileNum {
    
    //    電信號(hào)段:133/153/180/181/189/177
    //    聯(lián)通號(hào)段:130/131/132/155/156/185/186/145/176
    //    移動(dòng)號(hào)段:134/135/136/137/138/139/150/151/152/157/158/159/182/183/184/187/188/147/178
    //    虛擬運(yùn)營(yíng)商:170
    
    NSString *MOBILE = @"^1(3[0-9]|4[57]|5[0-35-9]|8[0-9]|7[06-8])\\d{8}$";
    
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    
    return [regextestmobile evaluateWithObject:mobileNum];
}
#pragma mark - 輸入只能是數(shù)字
+ (BOOL)validateNumber:(NSString*)number {
    BOOL res = YES;
    NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
    int i = 0;
    while (i < number.length) {
        NSString * string = [number substringWithRange:NSMakeRange(i, 1)];
        NSRange range = [string rangeOfCharacterFromSet:tmpSet];
        if (range.length == 0) {
            res = NO;
            break;
        }
        i++;
    }
    return res;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容