The specific implementation method of TextField and TextView limiting input length is for your reference. The specific content is as follows
TextField's restricted proxy method
Just code like code in this proxy method 16 The length can be set by yourself
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { NSInteger existedLength = ; NSInteger selectedLength = ; NSInteger replaceLength = ; NSInteger pointLength = existedLength - selectedLength + replaceLength; //If you have more than 16 bits, you can't enter it if (pointLength > 16) { return NO; }else{ return YES; } }
TextView's restricted proxy method
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text { //This judgment is equivalent to the proxy method of click return in textfield if ([text isEqualToString:@"\n"]) { [textView resignFirstResponder]; return NO; } //During the input process, determine whether the characters added to the input exceed the limited number of words. NSString *str = [NSString stringWithFormat:@"%@%@", , text]; if ( > 500) { = [ substringToIndex:500]; return NO; } return YES; }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.