输入四个字符后如何编写UItextfield长度和更改键盘数字格式的条件
Posted
技术标签:
【中文标题】输入四个字符后如何编写UItextfield长度和更改键盘数字格式的条件【英文标题】:how to write the condition for UItextfield length and changing keyboard number format after entering four char 【发布时间】:2017-03-08 06:25:43 【问题描述】:在 Uitextfield 长度 6 中,前四个字符必须是字符串,当我输入前四个字符时,两个必须是数字,我尝试输入第 5 个字符键盘更改为数字格式,那么我如何在目标 c 中编写上述条件。
【问题讨论】:
【参考方案1】:- (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;
【讨论】:
【参考方案2】:func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool
您可以使用此委托函数并根据您的要求添加所有必要条件。下面我展示了一些示例通用代码。您也可以键盘根据文本长度输入
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
【讨论】:
【参考方案3】:可以使用其属性keyboardType
以编程方式更改键盘类型。由于您必须检查每个输入,您需要实现shouldChangeCharactersInRange
的UITextFieldDelegate
的委托方法。
以下是一个示例,您可以对其进行优化以减少重新加载的次数。
- (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;
【讨论】:
【参考方案4】:-(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;
【讨论】:
以上是关于输入四个字符后如何编写UItextfield长度和更改键盘数字格式的条件的主要内容,如果未能解决你的问题,请参考以下文章