我希望改变一个富文本UITextView(iOS 6)的一些或所有属性文本,并允许用户撤销更改。
阅读了NSUndoManager documentation后,我尝试了第一种方法:
通过调用以下方法更改我的UITextView中的文本:
所以我尝试了第二种方式:
声明这个:
这里提出的一个技巧是模拟粘贴操作。如果可能的话,我更倾向于避免这样做(目前没有理由,只是感觉这样太不干净了,以后可能会造成麻烦)。
阅读了NSUndoManager documentation后,我尝试了第一种方法:
“Simple undo” based on a simple selector with a single object argument.
我希望撤销操作简单明了,只需要:
声明这个方法:
- (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];
}
}
通过调用以下方法更改我的UITextView中的文本:
[self setAttributedStringToTextView:mutableAttributedString];
但是这样做后,NSUndoManager说它无法撤销。
NSLog(@"Can undo: %d", [self.textView.undoManager canUndo]);
// Prints: "Can undo: 0"
所以我尝试了第二种方式:
“Invocation-based undo” which uses an NSInvocation object.
声明这个:
- (void)setMyTextViewAttributedString:(NSAttributedString *)newAttributedString {
NSAttributedString *currentAttributedString = [self.textView attributedText];
if (! [currentAttributedString isEqualToAttributedString:newAttributedString]) {
[[self.textView.undoManager prepareWithInvocationTarget:self]
setMyTextViewAttributedString:currentAttributedString];
[self.textView.undoManager setActionName:@"Attributed string change"];
[self.textView setAttributedText:newAttributedString];
}
}
并使用以下文本进行更改:
[self setMyTextViewAttributedString:mutableAttributedString];
之后,NSUndoManager也说它无法撤消。
为什么?
请注意,当触发更改属性文本的代码时,用户正在编辑UITextView。
一种解决方法是通过UITextInput协议方法直接替换文本。以下方法非常方便,但我没有找到NSAttributedString的等效方法。我错过了吗?
- (void)replaceRange:(UITextRange *)range withText:(NSString *)text
这里提出的一个技巧是模拟粘贴操作。如果可能的话,我更倾向于避免这样做(目前没有理由,只是感觉这样太不干净了,以后可能会造成麻烦)。
self.textView
、self.textView.attributedText
和self.textView.undoManager
是否都不为nil
? - Peter Hosey