TextField and TextView limit the length of input words
- 2020-10-31 21:59:58
- OfStack
TextField and TextView limit the input length of the specific implementation method, for your reference, the specific content is as follows
TextField's restricted proxy method
You just need to have code like code in this proxy method and 16 is the length you can set yourself
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSInteger existedLength = textField.text.length;
NSInteger selectedLength = range.length;
NSInteger replaceLength = string.length;
NSInteger pointLength = existedLength - selectedLength + replaceLength;
// More than 16 position I can't type it in
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 textfield The click return Proxy method of
if ([text isEqualToString:@"\n"]) {
[textView resignFirstResponder];
return NO;
}
// During the input process Determine the character input Whether the word limit is exceeded
NSString *str = [NSString stringWithFormat:@"%@%@", textView.text, text];
if (str.length > 500)
{
textView.text = [textView.text substringToIndex:500];
return NO;
}
return YES;
}