UITextView撤消管理器不适用于替换属性字符串(iOS 6) [英] UITextView undo manager do not work with replacement attributed string (iOS 6)

查看:196
本文介绍了UITextView撤消管理器不适用于替换属性字符串(iOS 6)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

iOS 6已经更新为使用UITextView进行富文本编辑(UITextView现在可以获得一个属性非文本的属性 - 这是非常可变的 - )。这是在NDA上的iOS 6 Apple论坛上提出的一个问题,可以公开,因为iOS 6现已公开...

iOS 6 has been updated to use UITextView for rich text editing (a UITextView now earns an attributedText property —which is stupidly non mutable—). Here is a question asked on iOS 6 Apple forum under NDA, that can be made public since iOS 6 is now public...

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

In a UITextView, I can undo any font change but cannot undo a replacement in a copy of the view's attributed string. When using this code...

- (void) replace: (NSAttributedString*) old with: (NSAttributedString*) new

{
1.    [[myView.undoManager prepareWithInvocationTarget:self] replace:new with:old];
2.    old=new;

}

...撤消工作正常。

... undoing is working well.

但是如果我添加一行来在我的视图中看到结果,则undoManager不会触发replace:with:方法,因为它应该......

But if I add a line to get the result visible in my view, the undoManager do not fire the "replace:with:" method as it should...

- (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];

}

有什么想法吗?我有任何替换方法的问题,使用范围与否,MutableAttributedString我试图在线2...

Any idea? I have the same problem with any of the replacement methods, using a range or not, for MutableAttributedString I tried to use on line "2"...

推荐答案

嗯,哇,我真的不希望这个工作!我找不到解决方案,所以我开始尝试任何事情......

Umm, wow I really didn't expect this to work! I couldn't find a solution so I just started trying anything and everything...

- (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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆