输入四个字符后,如何编写UITEXTFIELD长度和更改键盘编号格式的条件



在uitextfield长度6中,前四个char必须是字符串剩余两个必须是数字,当进入第一个char时,要输入第5个char键盘更改为数字格式,那么我如何写上面的目标c。

- (BOOL)textField:(UITextField *)textField
shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string
{
   // NSLog(@"%d",range);
 NSString * r = [NSString stringWithFormat:@"%d", range];
 NSLog(@"%@",r);
 if ([r  isEqual: @"4"])
 {
    [_txtView setKeyboardType:UIKeyboardTypeNumberPad];
    [self.view endEditing:YES];
    [_txtView becomeFirstResponder];
 }

 return YES;
}

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool

您可以使用此委托Func,并根据您的要求添加所有必要的条件。下面我显示了一些示例通用代码。此外,您可以根据文本长度

键盘类型
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool{

    //length condition and also trim the string before yu check for space and dot(.)
    if(textField.text?.characters.count == 6){
        return false
    }
    //alphabetic condition
    if((textField.text?.characters.count <= 4)){
        if(string == "number"){
            return false
        }
    }else if((textField.text?.characters.count > 4  )){
        if(string == "alphabetic"  ){
            return false
        }
    }
    return true
}

键盘类型可以使用其属性keyboardType进行编程更改。由于您将必须检查所有要放置的输入,您需要实现shouldChangeCharactersInRangeUITextFieldDelegate委托方法。

下面是一个样本,您可以优化它以减少重新加载的数量。

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (textfield == <Your texfield>) { // this condition is required in case of multiple textfields in the same view else you can skip
        NSString *finalString = [textField.text stringByReplacingCharactersInRange:range withString:string];
        if (finalString.length <= 6) {
            if (finalString.length > 3) {
                textField.keyboardType = UIKeyboardTypePhonePad;
            } else {
                textField.keyboardType = UIKeyboardTypeDefault;
            }
            [textField reloadInputViews];
        } else {
            return NO;
        }
    }
}
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if (string.length == 0) {
    if (textField.text.length == 4) {
        textField.keyboardType = UIKeyboardTypeDefault;
        textField.resignFirstResponder;
        textField.becomeFirstResponder;
    }
} else {
    if (textField.text.length == 3) {
        textField.keyboardType = UIKeyboardTypeNumberPad;
        textField.resignFirstResponder;
        textField.becomeFirstResponder;
        return NO;
    } if (textField.text.length < 4) {
        NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:@"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"] invertedSet];
        NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
        return [string isEqualToString:filtered];
    } else if (textField.text.length == 6) {
        return YES;
    }
}
return true;

}

最新更新