• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Implementing a Find menu item like Safari? [SOLUTION]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Implementing a Find menu item like Safari? [SOLUTION]


  • Subject: Re: Implementing a Find menu item like Safari? [SOLUTION]
  • From: Ben Golding <email@hidden>
  • Date: Sun, 30 Jan 2011 22:05:54 +1100

On 28/01/2011, at 14:43, Ben Golding wrote:

> Safari has a really nice "Find" bar that scrolls down from the title pane of the window.  I'd like to do something like that in an application that I'm developing but I can't find out whether that's generally available and how to use it.

Thanks to couple of short but extremely informative e-mails from Uli Kusterer I know how to do this now.

The first thing to do is to wrap the content of the window in a single NSView.  In IB, select all in your window and then enclose that in a custom view which doesn't need a subclass.  We'll change the size of that view in the window to leave space at the top of the window's content view to leave space for the find "panel".

In IB, create a new window/panel and populate it with the NSSearchField and other buttons.  Hook those up to the actions/outlets/bindings in file's owner that trigger the appropriate actions and make it the size that you want it to be.  You need to add an outlet in File's owner to this window's view so we can add it to the window.

Here's the piece of code I used.  CsvContentView is the view enclosing the main window's content, findPanelView is the view containing my find panel components, csvWindow is the window containing csvContentView (I might have used the window manager to get this but a link using IB seemed easier).

- (void)makeFindPanelVisible
{
    NSRect contentRect = [csvContentView frame];

    [csvContentView setFrameSize:NSMakeSize(contentRect.size.width, contentRect.size.height - [findPanelView bounds].size.height)];
    [findPanelView setFrame:NSMakeRect(contentRect.origin.x, [[csvWindow contentView] frame].size.height - [findPanelView bounds].size.height,
                                       contentRect.size.width, [findPanelView bounds].size.height)];
    [[csvWindow contentView] addSubview:findPanelView];
}

- (IBAction)closeFindPanelAction:(id)sender
{
    NSRect contentRect = [csvContentView frame];

    // extra retain to stop findPanelView being released (for some reason)
    [[findPanelView retain] removeFromSuperviewWithoutNeedingDisplay];
    [csvContentView setFrameSize:NSMakeSize(contentRect.size.width, contentRect.size.height + [findPanelView bounds].size.height)];

    _isFindPanelVisible = NO;
}

I haven't implemented animation though I'd like to, I'm trying to get the functionality working first.

The second part of my question about highlighting the found string as Safari does can be done using -showFindIndicatorForRange:.  That method only exists on NSTextView so it's not obvious (to me) how to use that to highlight the matching field in an NSTableView which is what I'm using to display my data.  I'm just selecting the matching text instead.

Hope this helps someone.  There isn't much code but it takes a little fiddling to get it right.

	Ben.

Attachment: smime.p7s
Description: S/MIME cryptographic signature

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >Implementing a Find menu item like Safari? (From: Ben Golding <email@hidden>)

  • Prev by Date: Re: Initializing NSWindow with Carbon window
  • Next by Date: Help Book authoring tool?
  • Previous by thread: Re: Implementing a Find menu item like Safari?
  • Next by thread: what are the disadvantages in keeping libraries and input files inside app bundle
  • Index(es):
    • Date
    • Thread