UITextView限制字数与行数

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    CGFloat textHeight = textView.contentSize.height;
    NSLog(@"%@",[NSString stringWithFormat:@"%f",textHeight]);
    if (range.location > COMMENTMAXTEXT )//([textView.text length] + [text length] - range.length >250
    {
         _textView.text = [_textView.text substringToIndex:COMMENTMAXTEXT];
        return  NO;
    }
    else if (textView.contentSize.height >= COMMENTMAXROWHEIGHT  ){//正好是最8行
        
        textView.text = [textView.text substringToIndex:[textView.text length]-1];
        return NO;
    }
    else
    {
        return YES;
    }
}

- (void)textViewDidChange:(UITextView *)textView{
    
    if(textView.contentSize.height >= COMMENTMAXROWHEIGHT) {
        [MBProgressHUD showError:@"最多只能输入8行"];
        _textView.text = [textView.text substringToIndex:[textView.text length]-1];
    }else if(textView.text.length > COMMENTMAXTEXT){
        _textView.text = [_textView.text substringToIndex:COMMENTMAXTEXT];
          [MBProgressHUD showError:@"最多只能输入250个字"];
    }
    
//    NSLog(@"%s",__func__);
}

 

posted @ 2015-11-10 03:52  TheYouth  阅读(2128)  评论(0编辑  收藏  举报