【问题标题】:How to implement undo/redo in UIWebView如何在 UIWebView 中实现撤消/重做
【发布时间】:2015-08-10 12:20:42
【问题描述】:

我正在开发一个具有富文本编辑器功能的应用程序。在ZSSRichTextEditor 之上,我已经编写了我的编辑器代码。这里我的编辑器是 UIWebView,它将由 javascript 代码注入以支持/编辑富文本内容。

ZSSRichTextEditor 具有撤消/重做功能,但不符合我的要求。于是我开始自己实现撤销/重做功能。

在我通过UndoManager 之后,我开始知道实现撤消/重做不会那么令人头疼,因为 Apple 对我们有很大帮助。如果我们在适当的地方注册它,那么UndoManager 将处理所有其他事情。但在这里我正在努力如何/在哪里注册UndoManger 以进行可编辑的UIWebView

UITextView 中有很多实现撤消/重做的示例,但我没有找到任何可编辑的UIWebView

请有人指导我吗?

【问题讨论】:

  • 看看这个.. 希望它对你有用 [UIWebView with contentEditable][1] [1]: stackoverflow.com/questions/8474386/…
  • 摇动手机即可获得撤销/重做选项。
  • @sschunara 感谢您的评论!默认的撤消/重做无法正常工作,它会从编辑器中删除整个文本,但对我来说,撤消必须针对每个字符更改工作
  • @mohsin 当然,我还在等待解决方案:(

标签: ios objective-c uiwebview rich-text-editor nsundomanager


【解决方案1】:

首先,像这样为历史创建两个属性:

@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,这样您就不会在内存中拥有这些疯狂的长字符串。但这取决于内容在编辑器中的时间长度。希望这一切都有意义。

【讨论】:

  • 感谢您的回答!这肯定会奏效,但我相信应该有其他更好的方法来做到这一点。如果您查看 Evernote、OneNote 等应用程序,它们处理得非常好。我会再等几天才能得到合适的解决方案,否则我会采用这种方法:-)
  • 谢谢!我真的认为这是任何人都会实现重做/撤消的方式,因为您需要将状态保存在某种堆栈上。我不知道你会怎么做。当然,实际的委托方法依赖于您正在使用的库
猜你喜欢
  • 2016-02-18
  • 1970-01-01
  • 2012-07-02
  • 1970-01-01
  • 2012-12-16
  • 2011-11-14
  • 2011-03-10
  • 2011-04-03
  • 1970-01-01
相关资源
最近更新 更多