输入框字数限制实现

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哦"];
    }
}

你可能感兴趣的:(输入框字数限制实现)