1

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:

http://d.pr/i/w8wP

[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?

4

2 回答 2

2

对于您想要做的事情,您不需要使用 IB 或任何 find 操作,因为一个NSTextView对象(假设它的名称是myTextView)可以完成所有工作。您必须首先创建一系列要突出显示的字符,然后告诉myTextView这样做:

NSRange charRange = NSMakeRange( location length );   // whatever you want
[myTextView showFindIndicatorForRange:charRange];

如果选择在 a 内,NSScrollView则应使该选择可见。在致电之前执行此操作-showFindIndicatorForRange(请参阅下面的文档NSTextView):

NSLayoutManager *layout = [myTextView layoutManager];
NSRect rect = [layout boundingRectForGlyphRange:charRange
                                inTextContainer:[myTextView textContainer] ];
[myTextView scrollRectToVisible:rect];

现在是一个扩展,可以根据需要显示尽可能多的突出显示的字符串。假设我们要在素数位置标记所有字符。(我不知道这有什么好处,但为什么不呢?)。我们创建一个具有相应范围的 NSArray:(伪代码!,范围必须是 NSValues!)

allRanges = { (1,1) (2,1) (3,1) (5,1) (7,1) etc.}

现在可以一起选择所有内容:

[myTextView setSelectedRanges:allRanges];
// select the 3th (zero based) entry
[showFindIndicatorForRange:[allRanges objectAtIndex:3];

这在查找机制中使用,但它也可以在不使用任何查找过程的情况下工作。

于 2013-01-30T18:20:31.333 回答
0

在界面生成器中,您可以设置“查找”属性来控制内置查找/替换栏的显示位置。要在代码中执行查找操作,请performFindPanelAction:在 NSTextView 上调用。

于 2013-01-30T16:08:54.360 回答