美文网首页
输入框字数限制实现

输入框字数限制实现

作者: wg刚 | 来源:发表于2018-07-13 15:51 被阅读0次

UITextView

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFiledEditChanged:) name:@"UITextViewTextDidChangeNotification" object:self.textView];

UITextField

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFiledEditChanged:) name:@"UITextFieldTextDidChangeNotification" object:self.nameField];

实现:

-(void)textFiledEditChanged:(NSNotification *)obj
{
    UITextView *textField = (UITextView *)obj.object;
    NSString *toBeString = textField.text;
    if (toBeString.length > 500) {
        textField.text = [toBeString substringToIndex:500];
        [MBProgressHUD showMessage:@"字数不能超过500哦"];
    }
}

相关文章

网友评论

      本文标题:输入框字数限制实现

      本文链接:https://www.haomeiwen.com/subject/ovxspftx.html