6

バックグラウンド

そのため、iOS 6 では、UITextView は attributedString を取ることができます。これは、構文の強調表示に役立ちます。

私はいくつかの正規表現パターンを実行していますが-textView:shouldChangeTextInRange:replacementText:、多くの場合、既に入力された単語の色を変更する必要があります。時間がかかる attributedText をリセットする以外に選択肢はありません。

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    //A context will allow us to not call -attributedText on the textView, which is slow.
    //Keep context up to date
    [self.context replaceCharactersInRange:range withAttributedString:[[NSAttributedString alloc] initWithString:text attributes:self.textView.typingAttributes]];

    // […]

    self.textView.scrollEnabled = FALSE;

    [self.context setAttributes:self.defaultStyle range:NSMakeRange(0, self.context.length)];
    [self refresh]; //Runs regex-patterns in the context
    textView.attributedText = self.context;

    self.textView.selectedRange = NSMakeRange(range.location + text.length, 0);
    self.textView.scrollEnabled = TRUE;

    return FALSE;
}

これはシミュレーターでは問題なく実行されますが、iPad 3 ではそれぞれ-setAttributedText数百ミリ秒かかります。

attributedText を変更できるようにするようにという要求で、Apple にバグを報告しました。重複としてマークされたので、彼らがこれについて何を言っているのかわかりません.

質問

より具体的な質問: UITextView の特定の範囲の色を変更するにはどうすればよいshouldReplaceText...ですか?

より広範な質問: iOS 6 で UITextView を使用して構文の強調表示を行うにはどうすればよいですか?

4

2 に答える 2

0

attributedText アクセサーは HTML との間でラウンドトリップする必要があるため、構文が強調表示されたテキスト ビューの実装には最適ではありません。iOS 6 では、おそらく CoreText を直接使用したいと思うでしょう。

于 2012-10-13T05:32:38.217 に答える