2012-12-12 2 views
4

내가 검색 바 ([textView setUsesFindBar:YES];)를 사용하는 NSTextView을 검색 문자열과 명확한 시각적 피드백을 설정합니다.NSTextFinder는 프로그래밍 방식

나는 2 개의 질문이 있습니다.

  1. 찾기 작업에서 시각적 피드백을 어떻게 지울 수 있습니까? 내가 프로그래밍 텍스트 뷰의 내용을 변경할 때

    내 문제가 발생합니다. 이전 내용에 대한 검색 작업에 대한 시각적 피드백은 내용이 변경된 후에도 그대로 유지됩니다. 분명히이 노란색 상자는 새 내용에 적용되지 않으므로 textView 내용을 변경할 때 내용을 지울 수있는 방법이 필요합니다.

    참고 : 나는 간단한 텍스트 뷰 그냥 다른 노력없이 작동하는 검색 바 있기 때문에 나는 NSTextFinderClient 프로토콜을 구현하지 않았다.

  2. 어떻게 찾기 표시 줄에 검색 문자열을 보낼 수 ?

답변

11

답을 찾았으니 여기에 다른 방법이 있습니다. 당신이 그것을 제어 할 수

먼저 당신은 NSTextFinder의 인스턴스가 필요합니다. 우리는 이것을 코드로 설정했습니다.

textFinder = [[NSTextFinder alloc] init]; 
[textFinder setClient:textView]; 
[textFinder setFindBarContainer:[textView enclosingScrollView]]; 
[textView setUsesFindBar:YES]; 
[textView setIncrementalSearchingEnabled:YES]; 

첫 번째 대답 : 나는이 두 가지 중 하나를 할 수있는 시각적 인 피드백을 취소합니다. 난 그냥

[textFinder cancelFindIndicator]; 

... 시각적 피드백을 취소 할 수 있습니다 또는 나는 내 텍스트 뷰의 내용을 변경하려고 해요 NSTextFinder ...

[textFinder noteClientStringWillChange]; 

두 번째 대답을 경고 할 수있다 글로벌 NSFindPboard. 이를 사용하여 검색을 설정할 수 있습니다.

// change the NSFindPboard NSPasteboardTypeString 
NSPasteboard* pBoard = [NSPasteboard pasteboardWithName:NSFindPboard]; 
[pBoard declareTypes:[NSArray arrayWithObjects:NSPasteboardTypeString, NSPasteboardTypeTextFinderOptions, nil] owner:nil]; 
[pBoard setString:@"new search" forType:NSStringPboardType]; 
NSDictionary* options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], NSTextFinderCaseInsensitiveKey, [NSNumber numberWithInteger:NSTextFinderMatchingTypeContains], NSTextFinderMatchingTypeKey, nil]; 
[pBoard setPropertyList:options forType:NSPasteboardTypeTextFinderOptions]; 

// put the new search string in the find bar 
[textFinder cancelFindIndicator]; 
[textFinder performAction:NSTextFinderActionSetSearchString]; 
[textFinder performAction:NSTextFinderActionShowFindInterface]; // make sure the find bar is showing 

그래도 문제가 있습니다. 검색 막대의 실제 텍스트 필드는 해당 코드 다음에 업데이트되지 않습니다. 나는 ... 나는 첫 번째로 반응을 전환하면 그때가 업데이트받을 수 있다는 것을 발견

[myWindow makeFirstResponder:outlineView]; 
[myWindow makeFirstResponder:textView];