限制字数的textview

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text

{

if ([textView.text isEqualToString:@"请详细的描述您遇到的问题,并留下您的手机号等联系方式,以方面我们与您联系!"])

{

textView.text=@"";

textView.textColor=RGB(51, 51, 51);

}

_optionNumberLabel.text=[NSString stringWithFormat:@"%lu/%d",(unsigned long)textView.text.length - range.length + text.length,200];

//获取当前文本框文字的数量

if ((textView.text.length - range.length + text.length) > MY_MAX)

{

NSString *substring = [text substringToIndex:MY_MAX - (textView.text.length - range.length)];

NSMutableString *lastString = [textView.text mutableCopy];

[lastString replaceCharactersInRange:range withString:substring];

textView.text = [lastString copy];

[MBProgressHUD showError:@"字数最多200字"];

return NO;

}

else

{

return YES;

}

}

- (void) textViewDidChange:(UITextView*)textview

{

self.placeLabel.hidden = textview.text.length > 0;

}

你可能感兴趣的:(限制字数的textview)