- (void)textViewDidChange:(UITextView *)textView{
//placeholder设置
if (textView.text.length==0) {
_placeholder.hidden = NO;
}else{
_placeholder.hidden = YES;
}
//限制字数为60,不管是粘贴还是自己输入都会截取60个字符
if (textView.text.length>60) {
textView.text = [textView.text substringToIndex:60];
}
_countLabel.text = [NSString stringWithFormat:@"%lu/60",(unsigned long)textView.text.length];
NSLog(@"%@",textView.text);
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
if ([text isEqualToString:@"\n"]) {
[textView resignFirstResponder];
}
//限制字数最好不要用这个方法
// if (textView.text.length+text.length>60) {
// return NO;
// }
return YES;
}
转载于:https://my.oschina.net/wenchengxu123/blog/1583259