I'm developing a MacOs application and have to implement my own search functional due to custom interface.
So the question is - how can a draw a border around a part of NSAttributedString in NSTextView to display search result (just how it's done in TextEdit app). Please check the desired look on the attached image:
[UPDATE]
Due to custom interface requirements I can't use NSTextViews's setUsesFindBar:
and setUsesFindPanel:
. The interface has a custom NSSearchField, which should perform search in a textView.
I've got familiar with performTextFinderAction:
, but how do I trigger it on a textView with the NSSearchField value?
For what you want to do you need not use the IB or any find operation, because an
NSTextView
object (let's say its name ismyTextView
) does it all. You have to first create a range of characters you want to highlight and then tellmyTextView
to do it:If the selection is within a
NSScrollView
this selection should be made visible. Do this before you call-showFindIndicatorForRange
(see the doc underNSTextView
):And now an extension to show as many highlighted strings as you want. Let us assume we want to mark all characters at a prime number position. (I do not know what that shall be good for, but why not?). We create an NSArray with the corresponding ranges: (pseudocode!, the ranges must be NSValues! )
All can now be selected together:
This is used within the find mechanism, but it also works without using any finding process.
In interface builder, you can set the "Find" property to control where the built in find/replace bar is displayed. To do a find action in code, invoke
performFindPanelAction:
on the NSTextView.