IOS 限制UITextView的输入字数

没什么难度直接上代码了。

首先是设置UITextView的委托:

[textView setDelegate:self];

之后完成下面改变事件:

- (BOOL)textView:(UITextView *)atextView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    NSString *new = [textView.text stringByReplacingCharactersInRange:range withString:text];
    NSInteger res = TEXT_MAXLENGTH-[new length];
    if(res >= 0){
        return YES;
    }
    else{
        NSRange rg = {0,[text length]+res};
        if (rg.length>0) {
            NSString *s = [text substringWithRange:rg];
            [textView setText:[textView.text stringByReplacingCharactersInRange:range withString:s]];
        }
        return NO;
    }
}



你可能感兴趣的:(ios,ios,ios,Objective-C,Objective-C,Objective-C,Objective-C,iPhone,iPhone,iPhone,iPhone,iPhone)