美文网首页
iOS监听键盘 限制输入字数 (待完善)

iOS监听键盘 限制输入字数 (待完善)

作者: coderJerry01 | 来源:发表于2020-01-10 05:39 被阅读0次
      监听键盘 限制输入字数
     [self.inputTextField addTarget:self action:@selector(textfieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    - (void)textfieldDidChange:(UITextField *)textField{
    UITextRange *selectedRange = [textField markedTextRange];
    NSString *newText = [textField textInRange:selectedRange];
    if (!newText.length) {
        if (self.inputTextField.text.length > 10) {
            self.inputTextField.text = [self.inputTextField.text substringToIndex:10];
            self.characterNumLab.text = @"10/10";
            return;
        }
        self.inputTextField.text = textField.text;
        NSInteger numCount = textField.text.length;
        self.characterNumLab.text = [NSString stringWithFormat:@"%ld/10", (long)numCount];
      }
    }

    相关文章

      网友评论

          本文标题:iOS监听键盘 限制输入字数 (待完善)

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