2012-09-19 27 views
3

iOS 6已更新爲使用UITextView進行富文本編輯(UITextView現在獲得了一個屬性文本屬性 - 這是愚蠢的不可變的)。這裏是一個問題在iOS 6蘋果論壇下的NDA問題,可以公開,因爲iOS 6現在是公開的...UITextView撤消管理器不能使用替換屬性字符串(iOS 6)

在UITextView中,我可以撤消任何字體更改,但不能撤消副本中的替換視圖的歸因字符串。使用此代碼時...

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

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

} 

...撤銷運行良好。

但是,如果我添加一行來得到我的觀點的結果可見,在UndoManager的不火的「替代:用」方法,因爲它應該...

- (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」...

回答

0

撤銷管理器復位後設置其'文本'或'屬性文本'財產,這就是爲什麼它不起作用。無論這種行爲是一個錯誤還是通過設計我都不知道。

但是,您可以改用UITextInput協議方法。

  • (無效)replaceRange:(UITextRange *)範圍withText:(的NSString *)文本

這工作。

+1

謝謝。但問題是這個協議不能代替一個歸屬串,只有一個NSString。 – Denis

1

嗯,哇我真的沒有想到這個工作!我找不到解決方案,所以我剛開始嘗試任何事情......

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