UITextView 撤消管理器不适用于替换属性字符串(iOS 6)

Den*_*nis 5 uitextview nsundomanager ios6

iOS 6 已更新为使用 UITextView 进行富文本编辑(UITextView 现在获得属性文本属性 \xe2\x80\x94,该属性是愚蠢的不可变\xe2\x80\x94)。这是在 NDA 下在 iOS 6 Apple 论坛上提出的问题,由于 iOS 6 现已公开,因此可以公开该问题......

\n\n

在 UITextView 中,我可以撤消任何字体更改,但无法撤消视图属性字符串副本中的替换。使用此代码时...

\n\n
- (void) replace: (NSAttributedString*) old with: (NSAttributedString*) new\n\n{\n1.    [[myView.undoManager prepareWithInvocationTarget:self] replace:new with:old];\n2.    old=new;\n\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

...撤消效果很好。

\n\n

但是,如果我添加一行以使结果在我的视图中可见,则 undoManager 不会触发“replace:with:”方法,因为它应该......

\n\n
- (void) replace: (NSAttributedString*) old with: (NSAttributedString*) new\n\n{\n1.    [[myView.undoManager prepareWithInvocationTarget:self] replace:new with:old];\n2.    old=new;\n3.    myView.attributedText=[[NSAttributedString alloc] initWithAttributedString:old];\n\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

任何想法?我对任何替换方法都有同样的问题,无论是否使用范围,对于我尝试在“2”行使用的 MutableAttributedString...

\n

小智 0

撤消管理器在设置其“text”或“attributedText”属性后会重置,这就是它不起作用的原因。我不知道这种行为是错误还是设计使然。

但是,您可以改用 UITextInput 协议方法。

  • (void)replaceRange:(UITextRange *)range withText:(NSString *)text

这有效。