在NSAttributedString的一部分周围绘制边框 [英] Draw a border around a part of NSAttributedString

查看:669
本文介绍了在NSAttributedString的一部分周围绘制边框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发MacOs应用程序,由于需要自定义界面,因此必须实现自己的搜索功能。

I'm developing a MacOs application and have to implement my own search functional due to custom interface.

所以问题是-如何在周围绘制边框NSTextView中NSAttributedString的一部分以显示搜索结果(正是在TextEdit应用中完成此操作的方式)。请检查所附图像上的所需外观:

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

[更新]

由于自定义界面的要求,我不能使用NSTextViews的 setUsesFindBar: setUsesFindPanel:。该接口具有自定义的NSSearchField,应在textView中执行搜索。

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.

我已经熟悉 performTextFinderAction:,但是如何在带有NSSearchField值的textView上触发它?

I've got familiar with performTextFinderAction:, but how do I trigger it on a textView with the NSSearchField value?

推荐答案

对于您要执行的操作,您无需使用IB或任何查找操作,因为 NSTextView 对象(假设它的名称是 myTextView )可以完成所有操作。您必须首先创建要突出显示的字符范围,然后告诉 myTextView 做到这一点:

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 is myTextView) does it all. You have to first create a range of characters you want to highlight and then tell myTextView to do it:

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

如果选择在 NSScrollView 之内选择应该是可见的。在调用 -showFindIndicatorForRange 之前执行此操作(请参见 NSTextView 下的文档):

If the selection is within a NSScrollView this selection should be made visible. Do this before you call -showFindIndicatorForRange (see the doc under NSTextView):

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

现在是扩展名,可以显示任意多个突出显示的字符串。让我们假设我们要标记所有字符在质数位置。 (我不知道这对您有什么好处,但是为什么不呢?)。我们使用相应的范围创建一个NSArray:(伪代码 !,范围必须为NSValues!)

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! )

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

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

All can now be selected together:

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

这在查找机制中使用,但是它也可以不使用任何查找过程而工作。

This is used within the find mechanism, but it also works without using any finding process.

这篇关于在NSAttributedString的一部分周围绘制边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆