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

标签 uitextview ios6 nsundomanager

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/

相关文章:

ios - SWIFT:在 uiscrollview 中无法按下按钮

iphone - UITableViewCell 中的 UITextView 平滑自动调整大小显示和隐藏 iPad 上的键盘,但适用于 iPhone

ios - 如何在ios中的下拉列表顶部带一个小箭头

cocoa - TextView 的多个撤消管理器

ios - UITableView滚动时如何退出textview

iphone - 从ios中的应用程序转到应用程序商店时如何返回应用程序

objective-c - 微博等iOS 6 ActivityViewController定制

swift - NSResponder 子类中的 UndoManager (macOS)

iphone - 撤消在 subview Controller 中所做的所有更改

ios - UITextView 中的简单文本格式