NSTextView和突出显示链接 [英] NSTextView and highlighting links

查看:85
本文介绍了NSTextView和突出显示链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 NSTextView NSAttributedString 使用显示一些链接NSLinkAttributeName 。我正在使用 NSTextView setLinkTextAttributes:方法设置样式。但是,我想给用户单击链接时提供视觉反馈,例如通过在用户按下鼠标左键时更改链接的颜色。有没有简便的方法或必须使用 NSTextAttachment

I'm using a NSTextView with an NSAttributedString to display some links using the NSLinkAttributeName. I'm styling those links using the setLinkTextAttributes: method of NSTextView. However, I would like to give the user a visual feedback when he clicks the link, for example by changing the color of the link while the user pressed down the left mouse button. Is there an easy way to do that or do I have to use NSTextAttachment?

推荐答案

如果您希望它发生在mouseDown事件上,我认为没有简单的方法可以做到这一点。如果您可以等到单击完成,则可以使用文本视图委托方法textView:clickedOnLink:atIndex:来获得单击链接的索引。然后,您可以使用该索引传递给文本存储方法,attributesAtIndex:effectiveRange:来获取链接的范围,最后使用setAttributes:range:来对链接的属性进行任何更改。

I don't think there is an easy way to do this if you want it to happen on the mouseDown event. If you can wait until the click is completed, then you can do it using the text view delegate method, textView:clickedOnLink:atIndex: to get the index of the clicked on link. You could then use that index to pass to the text storage method, attributesAtIndex:effectiveRange: to get the range of the link and finally, use setAttributes:range: to make whatever changes you want to the attributes of the link.

如果您确实需要在mouseDown上执行此操作,则必须对文本视图进行子类化并覆盖mouseDown,并可能使用诸如characterIndexForPoint之类的内容:来确定是否已单击在链接上并按上述方式进行响应。

If you really need to do it on the mouseDown, then you'll have to subclass the text view and override mouseDown and maybe use something like characterIndexForPoint: to find out whether you've clicked on a link and respond as above.

这篇关于NSTextView和突出显示链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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