限制输入及粘贴的总字数

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if ([@"" isEqualToString:text]) {
        return YES;
    }
    if (textView.text.length > kContentLimit) {
        return NO;
    }
    NSString *string = [textView.text stringByReplacingCharactersInRange:range withString:text];
    if ([string length] > kContentLimit)
    {
        string = [string substringToIndex:kContentLimit];
        textView.text = string;
        return NO;
    }
    return YES;
}

你可能感兴趣的:(限制输入及粘贴的总字数)