UITextView 长度限制

- (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 > MAX_INPUT_LENGTH) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"超出最大可输入长度" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
            [alert show];
            return NO;
        }
        else {
            return YES;
        }
    }
}

你可能感兴趣的:(UITextView 长度限制)