限制输入数字与指定位数,该点主要留意的是在返回 no 之后, textField 已经不能再进行操作, 每次输入会进入该方法. 但是因为键盘也是一样需要输入的, 同样经过该方法.
`
-
(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
BOOL isOperate = NO;
// 限制输入位
if (string.hash == 0) {
return YES;
} else {
if (textField.text.length < 5) {
NSCharacterSet *cSet = [[NSCharacterSet characterSetWithCharactersInString: @"0123456789"] invertedSet];
NSString *fil = [[string componentsSeparatedByCharactersInSet: cSet] componentsJoinedByString: @""];
isOperate = [string isEqualToString: fil];
}
}
return isOperate;
}
`