TextField and textView restrict input conditions. textfieldtextview
The following two methods are used to implement the proxy methods of textfield and textView.
I. textField only supports entering numbers and decimal places, and only up to five digits can be reserved after the decimal point.
-(BOOL) textField :( UITextField *) textField shouldChangeCharactersInRange :( nsange) range replacementString :( NSString *) string {// the value in the input box can only be two if (string) digits after the decimal point. length = 0) {return YES;} const char * ch = [string cStringUsingEncoding: NSUTF8StringEncoding]; if (* ch = 0) return YES; if (* ch! = 46 & (* ch <48 | * ch> 57) return NO; if ([textField. text rangeOfString :@". "]. length = 1) {NSUInteger length = [textField. text rangeOfString :@". "]. location; if ([textField. text substringFromIndex: length]> 2 | * ch = 46) return NO;} else {NSInteger existedLength = textField. text. length; NSInteger selectedLength = range. length; NSInteger replaceLength = string. length; if (existedLength-selec TedLength + replaceLength> 5 &&! [String isEqualToString: @ "."]) {return NO;} return YES ;}
2. textView can contain up to 25 words
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{ if ([text isEqualToString:@""] && range.length > 0) { return YES; }else { if (textView.text.length - range.length + text.length > 25) { return NO; }else { return YES; } }}