UITextView、UITextField限制字符数

 [self.textField addTarget:self action:@selector(textFileTextDidChange:) forControlEvents:UIControlEventEditingChanged];

- (void)textFileTextDidChange:(UITextField *)textField{

    UITextRange *selectedRange = textField.markedTextRange;

    UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];

    if (position) {

        return;

    }


    if (self.maxCount==0){

        return;

    }


    if (textField.text.length > self.maxCount) {

        textField.text = [textField.text substringWithRange:NSMakeRange(0, self.maxCount)];

    }

}

将textField替换成textView同样适用。

你可能感兴趣的:(UITextView、UITextField限制字符数)