iOS---只能输入两位小数

#pragma mark----初始化

- (void)initTextField{

_testTextField = [[UITextField alloc]init];

[self.view addSubview:_testTextField];

_testTextField.keyboardType =  UIKeyboardTypeDecimalPad;

_testTextField.frame = CGRectMake(100, 100, 200, 30);

_testTextField.backgroundColor = [UIColor yellowColor];

_testTextField.textColor = [UIColor redColor];

_testTextField.delegate =self;

_testTextField.placeholder = @"金额";

}

- (void)textFieldDidChange:(UITextField *)textField{

NSLog(@"%@",textField.text);

}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

if (textField == _testTextField) {

if ([string isEqualToString:@""]) return YES;

NSString *str = [NSString stringWithFormat:@"%.1f",[textField.text floatValue]*10];

if ([str integerValue] !=[textField.text floatValue]*10) {

return NO;

}

return YES;

}

return YES;

}

你可能感兴趣的:(iOS---只能输入两位小数)