我想要更改丰富的UITextView(iOS 6)的部分或全部属性文本,并允许用户撤消更改.
在阅读NSUndoManager文档后,我尝试了第一种方式:
“Simple undo” based on a simple selector with a single object argument.
Run Code Online (Sandbox Code Playgroud)
我期望撤消操作就像这样简单:
声明这个方法:
- (void)setAttributedStringToTextView:(NSAttributedString *)newAttributedString {
NSAttributedString *currentAttributedString = self.textView.attributedText;
if (! [currentAttributedString isEqualToAttributedString:newAttributedString]) {
[self.textView.undoManager registerUndoWithTarget:self
selector:@selector(setAttributedStringToTextView:)
object:currentAttributedString];
[self.textView.undoManager setActionName:@"Attributed string change"];
[self.textView setAttributedText:newAttributedString];
}
}
Run Code Online (Sandbox Code Playgroud)
通过调用以下命令更改我的UITextView中的文本:
[self setAttributedStringToTextView:mutableAttributedString];
Run Code Online (Sandbox Code Playgroud)
但在这之后,NSUndoManager表示它无法撤消.
NSLog(@"Can undo: %d", [self.textView.undoManager canUndo]);
// Prints: "Can undo: 0"
Run Code Online (Sandbox Code Playgroud)
所以我尝试了第二种方式:
“Invocation-based undo” which uses an NSInvocation object.
Run Code Online (Sandbox Code Playgroud)
声明:
- (void)setMyTextViewAttributedString:(NSAttributedString *)newAttributedString {
NSAttributedString *currentAttributedString = [self.textView attributedText];
if (! [currentAttributedString isEqualToAttributedString:newAttributedString]) …Run Code Online (Sandbox Code Playgroud) uitextview nsattributedstring nsundomanager uitextinput ios6
我正在尝试将撤消和重做功能添加到我的UITextView. 我使用的attributedText不仅仅是text的属性UITextView。我已经尝试使用Apple 文档undoManager中引用的函数调用,但是似乎没有任何反应。我很惊讶我无法通过谷歌搜索找到任何有关该主题的信息。有没有人在 / 上实现撤消和重做之前遇到过这个问题/知道如何解决这个问题?UITextViewattributedText
示例代码
textView.attributedText = NSMutableAttributedString(string: "SOME TEXT")
@objc func undo(_ sender: UIButton) {
textView.undoManager?.undo()
}
@objc func redo(_ sender: UIButton) {
textView.undoManager?.redo()
}
Run Code Online (Sandbox Code Playgroud)