我正在开发一个具有丰富的文本编辑功能的应用程序.在
ZSSRichTextEditor年以上我写了我的编辑器代码.这里我的编辑器是UIWebView,它将被JavaScript代码注入以支持/编辑富文本内容.
ZSSRichTextEditor具有撤销/重做功能,但不符合我的要求.所以我开始自己实现撤消/重做功能.
经过UndoManager之后,我知道执行撤消/重做将不会令人头疼,因为苹果对我们来说很有帮助.如果我们在适当的地方注册,那么UndoManager会照顾所有其他事情.但在这里,我正在努力如何/在哪里注册UndoManger来编辑UIWebView.
在UITextView中有很多示例来实现撤消/重做,但是我没有找到可编辑的UIWebView的任何东西
你能指点一下吗?
解决方法
首先,为历史创建两个属性:
- @property (nonatomic,strong) NSMutableArray *history;
- @property (nonatomic) NSInteger currentIndex;
那么我会做的是使用子类ZSSRichTextEditor,以便在按下一个键或者动作完成时得到委托调用.然后在每个代表电话中,您可以使用:
- - (void)delegateMethod {
- //get the current html
- NSString *html = [self.editor getHTML];
- //we've added to the history
- self.currentIndex++;
- //add the html to the history
- [self.history insertObject:html atIndex:currentIndex];
- //remove any of the redos because we've created a new branch from our history
- self.history = [NSMutableArray arrayWithArray:[self.history subarrayWithRange:NSMakeRange(0,self.currentIndex + 1)]];
- }
- - (void)redo {
- //can't redo if there are no newer operations
- if (self.currentIndex >= self.history.count)
- return;
- //move forward one
- self.currentIndex++;
- [self.editor setHTML:[self.history objectAtIndex:self.currentIndex]];
- }
- - (void)undo {
- //can't undo if at the beginning of history
- if (self.currentIndex <= 0)
- return;
- //go back one
- self.currentIndex--;
- [self.editor setHTML:[self.history objectAtIndex:self.currentIndex]];
- }
我还将使用某种FIFO(先进先出)方法来保持历史的大小小于20或30,这样您就不会在内存中有这些疯狂的长串.但是,这取决于编辑器中内容的长度.希望这一切都有道理.