输入框字数限制实现

2018-07-13  本文已影响0人  wg刚

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哦"];
    }
}
上一篇下一篇

猜你喜欢

热点阅读