4

iOS 6は、リッチテキスト編集にUITextViewを使用するように更新されました(UITextViewは、属性付きテキストプロパティを取得するようになりました。これは愚かなことに変更できません)。これは、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について、範囲を使用するかどうかに関係なく、どの置換メソッドでも同じ問題が発生します。

4

3 に答える 3

2

うーん、うわー、私は本当にこれがうまくいくとは思っていませんでした! 解決策が見つからなかったので、ありとあらゆることを試してみました...

- (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];
}
于 2014-06-17T13:10:27.997 に答える
0

Undo Manager は、'text' または 'attributedText' プロパティを設定した後にリセットされます。これが機能しない理由です。この動作がバグなのか仕様によるものなのかはわかりません。

ただし、代わりに UITextInput プロトコル メソッドを使用できます。

  • (void)replaceRange:(UITextRange *)range withText:(NSString *)テキスト

これは機能します。

于 2013-02-17T12:56:17.110 に答える