2

내 구문 강조 표시 구현의 경우 을 사용하여 NSTextView에 대한 변경 사항을 확인합니다.-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 ... 
} 

표시 할 문자의 범위를 가져오고 싶습니다.

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

가 어떻게 그렇게 내가 그들을 색상 수 표시 문자의 범위를 얻을 것이다 : 나는 그것을 충돌하게 백 스페이스을 명중 할 때까지 위의 코드는 작동?

+0

의시 editedRange''의 가치를 무엇을 추락? –

답변

2

확인 범위는 전체 문자열의 경계를 넘어, 그리고 경우는 문자열의 경계에 맞게 범위 설정되어있는 경우 :

NSRange range = NSRangeFromString(string);