iOS 6 已更新为使用 UITextView 进行富文本编辑(UITextView 现在获得了一个属性文本属性——这是愚蠢的不可变的——)。这是在 NDA 下在 iOS 6 Apple 论坛上提出的一个问题,由于 iOS 6 现已公开,因此可以公开...
在 UITextView 中,我可以撤消任何字体更改,但不能撤消 View 属性字符串副本中的替换。使用此代码时...
- (void) replace: (NSAttributedString*) old with: (NSAttributedString*) new
{
1. [[myView.undoManager prepareWithInvocationTarget:self] replace:new with:old];
2. old=new;
}
...撤消效果很好。
但是,如果我添加一行以使结果在我的 View 中可见,则 undoManager 不会触发“replace:with:”方法,因为它应该......
- (void) replace: (NSAttributedString*) old with: (NSAttributedString*) new
{
1. [[myView.undoManager prepareWithInvocationTarget:self] replace:new with:old];
2. old=new;
3. myView.attributedText=[[NSAttributedString alloc] initWithAttributedString:old];
}
任何的想法?对于我尝试在“2”行使用的 MutableAttributedString 的任何替换方法,无论是否使用范围,我都有同样的问题......
最佳答案
嗯,哇,我真的没想到这会奏效!我找不到解决方案,所以我开始尝试任何事情......
- (void)applyAttributesToSelection:(NSDictionary*)attributes {
UITextView *textView = self.contentCell.textView;
NSRange selectedRange = textView.selectedRange;
UITextRange *selectedTextRange = textView.selectedTextRange;
NSAttributedString *selectedText = [textView.textStorage attributedSubstringFromRange:selectedRange];
[textView.undoManager beginUndoGrouping];
[textView replaceRange:selectedTextRange withText:selectedText.string];
[textView.textStorage addAttributes:attributes range:selectedRange];
[textView.undoManager endUndoGrouping];
[textView setTypingAttributes:attributes];
}
关于UITextView 撤消管理器不适用于替换属性字符串(iOS 6),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12501541/