Recently, the company has involved the function of writer assistant, which can revoke input text and reversely revoke revoked cancelled text.
This function is similar to the ios system's shake and unplug input.
I was also very confused at that time and didn’t know where to start. Later, I searched for a lot of information and finally completed this function. Now I will write down the implementation of this function, and encourage each other.
This function involves the ios native class: NSUndomanager. This category is quite powerful. Without further ado, just upload the code.
#import "" @interface ViewController ()<UITextViewDelegate>{ UITextView *_textView; NSUndoManager *_undomanager; NSInteger _length; UIButton *undobutton; UIButton *redobutton; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; UIBarButtonItem *undoItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemUndo target:self action:@selector(undoitem)]; UIBarButtonItem *redoItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemRedo target:self action:@selector(redoitem)]; = undoItem; = redoItem; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardShow:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardHidden:) name:UIKeyboardWillHideNotification object:nil]; _length = 0; //Initialize NSUndoManager _undomanager = [[NSUndoManager alloc] init]; _textView = [[UITextView alloc]initWithFrame:CGRectMake(0, 200, , 400)]; _textView.backgroundColor = [UIColor yellowColor]; _textView.delegate = self; _textView.font = [UIFont systemFontOfSize:15]; _textView. = 5; _textView. = YES; _textView.textColor = [UIColor blackColor]; _textView.text = @" ";//Set the initial text, otherwise the paragraph will not be reflected. NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; = 5; //Line spacing = 30; /**First row indent width*/ = NSTextAlignmentLeft; NSDictionary *attributes = @{ NSFontAttributeName:[UIFont systemFontOfSize:13], NSParagraphStyleAttributeName:paragraphStyle }; _textView.attributedText = [[NSAttributedString alloc] initWithString:_textView.text attributes:attributes]; //Notification of listening to textview text changes [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeTextViewText) name:UITextViewTextDidChangeNotification object:nil]; [ addSubview:_textView]; } -(void)redoitem{ //Anti-revocation [_undomanager redo]; } -(void)undoitem{ //Revoke [_undomanager undo]; } -(void)keyBoardShow:(NSNotification *)noti{ NSDictionary *dic = ; NSValue *aValue = [dic objectForKey:UIKeyboardFrameEndUserInfoKey]; CGRect keyboardRect = [aValue CGRectValue]; int height = ; [_textView setContentInset:UIEdgeInsetsMake(0, 0, height, 0)]; } -(void)keyBoardHidden:(NSNotification *)noti{ [_textView setContentInset:UIEdgeInsetsMake(0, 0, 0, 0)]; } - (void)setMyObjectTitle:(NSString *)newTitle{ //Judge the current state of NSUndoManager, which is in a state of revocation or counter-revocation-(void)textViewDidChange:(UITextView *)textView if (_undomanager.isUndoing) { NSInteger length = ; if (_textView.>0) { //Get _textView.text = [_textView.text substringWithRange:NSMakeRange(0, _textView. - length)]; [_undomanager registerUndoWithTarget:self selector:@selector(setMyObjectTitle:) object:newTitle]; } }else if (_undomanager.isRedoing){ _textView.text = [_textView.text stringByAppendingString:newTitle]; [_undomanager registerUndoWithTarget:self selector:@selector(setMyObjectTitle:) object:newTitle]; }else{ NSString *currentText = _textView.text; if (newTitle != currentText) { _textView.text = currentText; [_undomanager registerUndoWithTarget:self selector:@selector(setMyObjectTitle:) object:newTitle]; }else{ _textView.text = newTitle; } } } -(void)changeTextViewText{ if (_textView.>0) { = YES; }else{ = NO; = NO; } NSString *text ; if (_length != 0) { NSInteger textLength = _textView.; if (textLength > _length) { NSInteger newLength = textLength - _length; text = [NSString stringWithFormat:@"%@",[_textView.text substringWithRange:NSMakeRange(_length, newLength)]]; }else{ text = _textView.text; } }else{ text = _textView.text; } _length = _textView.; [self setMyObjectTitle:text]; }
Summarize
The above is the implementation code for the first line of iOS UITextView indentation, uninstall input, and reverse uninstall input that the editor introduced to you. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!