1.textfield的键盘类型:只能输入数字和小数点
textField.keyboardType = UIKeyboardTypeDecimalPad;
2.注册代理
textField.delegate = self;
3.在代理方法中控制:(1)首位不能输入小数点;(2)只能输入一个小数点;(3)小数点后最多输入两位。
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSLog(@"string =%@==",string);
if ([string isEqualToString:@"."]&&(textField.text.length==0)) {
return NO;
}
if ([string isEqualToString:@"."]&&[textField.text containsString:@"."]) {
return NO;
}
if (![string isEqualToString:@""]&&[textField.text containsString:@"."]) {
NSArray *arr = [textField.text componentsSeparatedByString:@"."];
if (arr.count == 2) {
NSString *str = arr.lastObject;
if (str.length>=2) {
return NO;
}
}
}
return YES;
}