logo
down
shadow

How to use NSTextFinder programmatically?


How to use NSTextFinder programmatically?

Content Index :

How to use NSTextFinder programmatically?
Tag : macos , By : Jenuel
Date : November 04 2020, 03:01 PM

this will help I'd like to do a "find" operation in an NSTextView without using the built-in find bar. How can I programmatically set a search string and have the results highlighted inside the text view? , Here's my test code (hacky, experimental):
@interface ViewController ()

@property (strong) NSTextFinder *textFinder;
@property (weak) IBOutlet NSTextView *textView; // find Uses Bar, Incremental Searching is on
@property (weak) NSView *myFindBarView;
@property (weak) IBOutlet NSView *findBarContainerView; // hidden
@property BOOL barVisible;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.textFinder = [[NSTextFinder alloc] init];
    self.textFinder.incrementalSearchingEnabled = YES;
    self.textFinder.incrementalSearchingShouldDimContentView = YES;
    self.textFinder.client = (id<NSTextFinderClient>)self.textView;
    self.textFinder.findBarContainer = self;
    [self.textFinder performAction:NSTextFinderActionShowFindInterface];
}

- (IBAction)testSearchString:(id)sender {
    // action method of a Test button, searches for "found"
    [self.textFinder cancelFindIndicator];

    // search the subviews for a view of class NSSearchField
    __block __weak NSSearchField *(^weak_findSearchField)(NSView *);
    NSSearchField *(^findSearchField)(NSView *);
    weak_findSearchField = findSearchField = ^(NSView *view) {
        if ([view isKindOfClass:[NSSearchField class]])
            return (NSSearchField *)view;
        __block NSSearchField *foundView = nil;
        [view.subviews enumerateObjectsUsingBlock:^(NSView *subview, NSUInteger idx, BOOL *stop) {  
            foundView = weak_findSearchField(subview);
            if (foundView)
                *stop = YES;
        }];
        return foundView;
    };

    NSSearchField *searchField = findSearchField(self.myFindBarView);
    [searchField setStringValue:@"found"];
    // execute the action of the search field to confirm the new value and do a search
    [searchField sendAction:searchField.action to:searchField.target];
    /* add to select all
    [self.textFinder performAction:NSTextFinderActionSelectAll];
    */
}

// NSTextFinderBarContainer

- (void)findBarViewDidChangeHeight {
}

- (NSView *)findBarView {
    return self.myFindBarView;
}

- (void)setFindBarView:(NSView *)theView {
    self.myFindBarView = theView;
    if (theView) {
        NSRect frame = theView.frame;
        frame.size.width = self.findBarContainerView.bounds.size.width;
        theView.frame = frame;
        [self.findBarContainerView addSubview:theView];
    }
}

- (NSView *)contentView {
    return self.textView.enclosingScrollView.contentView;
}

- (BOOL)isFindBarVisible {
    return self.barVisible;
}

- (void)setFindBarVisible:(BOOL)theVisible {
    self.barVisible = theVisible;
}

@end

Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

NSTextFinder set search string and clear visual feedback programmatically


Tag : objective-c , By : Martin
Date : March 29 2020, 07:55 AM
help you fix your problem I found my answers, so for others here's how to do it.
First you need an instance of NSTextFinder so you can control it. We set that up in code.
textFinder = [[NSTextFinder alloc] init];
[textFinder setClient:textView];
[textFinder setFindBarContainer:[textView enclosingScrollView]];
[textView setUsesFindBar:YES];
[textView setIncrementalSearchingEnabled:YES];
[textFinder cancelFindIndicator];
[textFinder noteClientStringWillChange];
// 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];

NSTextFinder action on NSTextView


Tag : macos , By : goffi
Date : March 29 2020, 07:55 AM

Tag : objective-c , By : lewing
Date : March 29 2020, 07:55 AM

How do I get NSTextFinder to show up


Tag : objective-c , By : Sebastian Gift
Date : March 29 2020, 07:55 AM

NSTextFinder + programmatically changing the text in NSTextView


Tag : cocoa , By : user96271
Date : March 29 2020, 07:55 AM
Related Posts Related QUESTIONS :
  • clear command in my mac os terminal gives the following error
  • how to get the path of a localized plist under mac os x
  • Is there a way to trigger gesture events on Mac OS X?
  • XAMPP default folder on OSX
  • Qt4: QMenuBar/QMenu doesn't show up on Mac OS X
  • How do you dynamically allocate memory in Mac OS X assembly?
  • Bluetooth proximity hooks on OS X. How? (i.e., automatically lock screen if I move away)
  • How can I create a temp file with a specific extension in bash?
  • How to fix intellij keymap to old keymap after 2019 update Mac OS?
  • VSCode monospace fonts are not monospaced, how to fix this?
  • Unable to install React Native cli on Mac using npm
  • Does go-get run builds forked from current shell process with different environment variables?
  • Is there a way to set the output of an AppleScript to a certain color, and change depending on conditions?
  • Why is this functionKey selected when I press F1, F2, etc?
  • Is there a way to replace the native maximize button on the main window with fullscreen button?
  • Resizing Docker disk image to a non-standard size
  • How to batch "touch" files in subfolder with data from parent folder
  • How can I bypass an index error or have the script run again?
  • What is the first variables of my stack program?
  • Jenkins through docker: How to configure own host as agent for jenkins?
  • Is there a way to determine the size of a docker volume on my macOS machine?
  • How do I return the output of my Apple Script to the Status Bar in macOS?
  • Gitlab Client in Login Redirect Loop
  • launchd: Confusion on the sematics of bootstrap/bootout for controlling services on macOS
  • How to unminimize a minimized window (apple script not working with upgrade from Capitan to Mojave)
  • How to add LoginItems since macOS 10.11 when LSSharedFileListInsertItemURL is deprecated?
  • Cannot mount a volume without using sudo
  • Trouble displaying stl file in SceneKit with macOS
  • Using Unicast RTSP URIs via ffmpeg
  • How to copy intl.so to /usr/lib/php/extensions
  • Macbook crontab: "Service cannot load in requested session"
  • Is it possible to build Electron Apps on Mac Pro? Doesn't seem like it due to Bizarre GPU issue
  • IntelliJ IDEA can't open projects or add SDK on macOS Catalina
  • OpenMPI on MacOS: Unexpected IP
  • The requested version of macOS is not available. HOW TO FIX IT TUTORIAL. macOS Catalina public beta
  • Mac Terminal: Looping & ImageMagick
  • SwiftUI scroll view partially displays last item in Mac app
  • AFP and Time Machine stopped working on QNAP HS-251+
  • How to add an application shortcut in dock on mac machine?
  • macOS: What's the correct place to install a dylib on a user's system?
  • Apache ERR_CONNECTION_REFUSED on Mac OS X Sierra 10.12
  • How to reconnect to lost VNC server on mac
  • Setting NSView readonly-property tag on subclass
  • How to install wget in macOS?
  • Command line tool running slowly from NSTask but fast from Terminal
  • AppleScript for gathering all windows error: Expected end of line but found unknown token
  • Get exact time of the file added to the folder
  • Basic event handling with core graphics
  • How to get the current workspace programmatically on macOS
  • Removing trailing new line after printing on Mac OS X zsh
  • How do I use this basic shell script in Mac OS X Terminal?
  • Open terminal and run commands on Mac User Login
  • Determining which application is reading or writing data from within a Mac OS filter scheme driver
  • How to increase quantity of file descriptors on MacOS Mojave?
  • How to replace a string in a file selected by a multi-line regex search pattern on Unix/MacOS with command line tools
  • OSX UI testing with XCUIApplication and covered windows
  • Remove MongoDB from auto starting on login
  • OS X Automator script : attach files to an email
  • Finding and uninstalling PIL on Mac
  • Control-click does not change selection in IKImageBrowserView, right-click or two-finger-click does
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com