位数限制: limited
在UITextField的代理方法中添加类似如下代码 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { NSMutableString * futureString = [NSMutableString stringWithString:textField.text]; [futureString insertString:string atIndex:range.location]; NSInteger flag=0; const NSInteger limited = 1; for (int i = futureString.length-1; i>=0; i--) { if ([futureString characterAtIndex:i] == ‘.‘) { if (flag > limited) { return NO; } break; } flag++; } return YES; }
时间: 2024-11-05 13:30:37