// 将UITextField的Keyboard设置为Number Pad
// viewController实现UITextFieldDelegate
//设置文本框只能输入数字
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
//如果是限制只能输入数字的文本框
if (_moneyTextField==textField) {
return [self validateNumber:string];
}
//否则返回yes,不限制其他textfield
return YES;
}
- (BOOL)validateNumber:(NSString*)number {
BOOL res =YES;
NSCharacterSet* tmpSet = [NSCharacterSetcharacterSetWithCharactersInString:@"0123456789"];
int i =0;
while (i < number.length) {
NSString * string = [numbersubstringWithRange:NSMakeRange(i,1)];
NSRange range = [stringrangeOfCharacterFromSet:tmpSet];
if (range.length ==0) {
res =NO;
break;
}
i++;
}
return res;
}
原文章出处:http://blog.csdn.net/iotjin/article/details/49932977。
网友评论