Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to I highlight search results in a webview like Safari and NSTextView showFindIndicatorForRange:

Tags:

cocoa

webkit

In Safari, and in an NSTextView on OSX, search results can be highlighted with a bright yellow box that has a little animated pop. Is there any way to do this in a webview without coding it all myself?

like image 450
Mike T Avatar asked Jan 05 '11 07:01

Mike T


2 Answers

I did find a way to do this.

See showFindIndicatorForRange:.

like image 149
Donovan Avatar answered Nov 15 '22 06:11

Donovan


The answer (as of 2013-05) seems to be that you have to highlight the find results in the webview manually (ref: "Implementing a Find menu item like Safari?" thread from 2011), although it's a long-planned webkit feature.

I'll list the links I found while researching this.

  • There's HighlightedWebView but at the time of writing it appears to insert <span>s to highlight the search terms.
  • I couldn't find an answer on how to mimic this effect with Core Animation:
    • Core animation - Safari Find effect on cocoa-dev has an overview of the solution, but no code
    • https://stackoverflow.com/questions/12187511/mimicking-showfindindicatorforrange-with-calayer here on SO has no answers
  • On WebView and NSTextFinder (the standard find bar interface):
    • How do I get NSTextFinder to show up here on SO is related, but it doesn't seem it's about this exact problem.
    • NSTextFinder reference (the "Text View Support for the Find Bar" section) suggests it can draw "the yellow find indicator background bezel" for any NSView/NSScrollView if it supports NSTextFinderClient (which WebView does not - ref.: [1], [2]).
    • "WebView find panel/bar implementation" on cocoa-dev is the most recent (2013-04) thread on this and it contains an overview of the possible solution.
like image 2
Nickolay Avatar answered Nov 15 '22 08:11

Nickolay