-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
if (textField == self.tyt_InputTextField){
NSInteger length = 7;
if (self.config.maxContentLength >0) {
length = self.config.maxContentLength;
}
if (self.config) {
if (textField.text.length == 0&&[string isEqualToString:@"."]) {
textField.text = @"0";
}
if (self.config.isInt == YES) {
if ([string isEqualToString:@"."]) {
return NO;
}
}
if ([textField.text isEqualToString:@"0"]&&![string isEqualToString:@"."]&&range.location != 0) {
return NO;
}
if ([textField.text rangeOfString:@"."].location != NSNotFound||[string isEqualToString:@"."]) {
length = 11;
if (self.config.maxContentLength>0) {
length = self.config.maxContentLength + 4;
if (self.config.isFloat == YES) {
length = self.config.maxContentLength + 1 + 4;
}
}
}
if ([self decimalPointTextFiled:textField shouldChangeCharactersInRange:range replacementString:string] == NO) {
return NO;
}
}
if (string.length == 0) return YES;
NSInteger existedLength = textField.text.length;
NSInteger selectedLength = range.length;
NSInteger replaceLength = string.length;
if (existedLength - selectedLength + replaceLength > length){
return NO;
}
}
return YES;
}
- (BOOL)decimalPointTextFiled:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
// 判断当前是否有小数点
if([textField.text rangeOfString:@"."].location!=NSNotFound){
if ([string isEqualToString:@"."]){
return NO;
}
}
// 首位不可重复输入0
if ([textField.text isEqualToString:@"0"] && [string isEqualToString:@"0"])
{
return NO;
}
//拼出输入完成的str,判断str的长度大于等于“.”的位置+4,则返回false,此次插入string失败 ("379132.424",长度10,"."的位置6, 10>=6+4)
NSMutableString *str = [[NSMutableString alloc] initWithString:textField.text];
[str insertString:string atIndex:range.location];
if (self.config.isFloat == YES&& self.config.maxContentLength == 2) {
if (str.length >= [str rangeOfString:@"."].location + 3){
return NO;
}
}else{
if (str.length >= [str rangeOfString:@"."].location + 4){
return NO;
}
}
return YES;
}