很原始 很粗暴的写法
定义输入最大的字符数
#define MAX_LIMIT_NUMS 360
遵循<UITextViewDelegate>
@interface MineIntrduceVC ()<UITextViewDelegate>
// 没有内容时候的 站位Lab
@property (weak, nonatomic) IBOutlet UILabel *placeHolderLab;
// 输入框
@property (weak, nonatomic) IBOutlet UITextView *textInputView;
// 显示当前输入字符
@property (weak, nonatomic) IBOutlet UILabel *textNumLab;
别忘了遵循代理
_textInputView.delegate = self;
代理方法
-(void)textViewDidBeginEditing:(UITextView *)textView{
_placeHolderLab.hidden = YES;
}
- (void)textViewDidChange:(UITextView *)textView{
//实时显示字数
self.textNumLab.text = [NSString stringWithFormat:@"%lu/360", (unsigned long)_textInputView.text.length];
//字数限制操作
if (_textInputView.text.length >= MAX_LIMIT_NUMS) {
_textInputView.text = [textView.text substringToIndex:MAX_LIMIT_NUMS];
_textNumLab.text = @"360/360";
}
}