3

私の構文強調表示の実装では、NSTextViewusingへの変更を観察し-[<NSTextStorageDelegate> textStorageDidProcessEditing:]ます。

- (void)textStorageDidProcessEditing:(NSNotification *)notification {
  if (!self.languageGrammar) return;
  NSTextStorage *textStorage = self.textView.textStorage;
  NSRange glyphRange = [self.textView.layoutManager glyphRangeForBoundingRect:self.scrollView.documentVisibleRect
                                                              inTextContainer:self.textView.textContainer];
  NSRange editedRange = [self.textView.layoutManager characterRangeForGlyphRange:glyphRange actualGlyphRange:NULL];

  [textStorage removeAttribute:NSForegroundColorAttributeName range:editedRange];
  // crash is the line above ^^^^
  // color text ...
}

表示可能な文字の範囲を取得したい。上記のコードはbackspace、ヒットするまで機能し、クラッシュします。

*** -[NSConcreteTextStorage attributesAtIndex:effectiveRange:]: Range or index out of bounds

表示される文字の範囲を取得して色を付けるにはどうすればよいですか?

4

1 に答える 1

2

範囲が文字列全体の境界を超えているかどうかを確認し、範囲が文字列の境界に収まるように設定されているかどうかを確認します。

NSRange range = NSRangeFromString(string);
于 2012-01-17T19:45:21.447 に答える