Monitor content change

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textEditChanged:) name:UITextFieldTextDidChangeNotification object:nil];
Copy the code


Processing constraints

-(void)textEditChanged:(NSNotification *)notification{
    UITextField *textField = (UITextField *)notification.object;
    if(textField ! = self.inputView) {return; } // The length to be limited NSUInteger maxLength = 7; NSString *contentText = textfield.text; UITextRange *selectedRange = [textField markedTextRange]; / / this line of code Get to highlight content can be thought of as the length of the NSInteger markedTextLength = [textField offsetFromPosition: selectedRange. Start toPosition:selectedRange.end]; // Operate on entered text when no content is highlightedif(markedTextLength == 0) {// If the content length of the text field is greater than the content length we limitif(contentText.length > maxLength) { NSRange rangeRange = [contentText rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, maxLength)]; textField.text = [contentText substringWithRange:rangeRange]; }}}Copy the code