iOS 6 已更新为使用 UITextView 进行富文本编辑(UITextView 现在获得一个 attributeText 属性,该属性是愚蠢的不可变)。这是在 NDA 下在 iOS 6 Apple 论坛上提出的问题,由于 iOS 6 现已公开,因此可以公开该问题......
在 UITextView 中,我可以撤消任何字体更改,但无法撤消视图属性字符串副本中的替换。使用此代码时...
- (void) replace: (NSAttributedString*) old with: (NSAttributedString*) new
{
1. [[myView.undoManager prepareWithInvocationTarget:self] replace:new with:old];
2. old=new;
}
...撤消效果很好。
但是,如果我添加一行以使结果在我的视图中可见,则 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];
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)