1.textfield的鍵盤類型:只能輸入數(shù)字和小數(shù)點(diǎn)
textField.keyboardType = UIKeyboardTypeDecimalPad;
2.注冊(cè)代理
textField.delegate = self;
3.在代理方法中控制:(1)首位不能輸入小數(shù)點(diǎn)杯缺;(2)只能輸入一個(gè)小數(shù)點(diǎn);(3)小數(shù)點(diǎn)后最多輸入兩位钠右。
-(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;
}