在NSTextView中创建一些文本的超链接 [英] Create hyperlink to some text in NSTextView
问题描述
我可以使用链接面板创建一个超链接到NSTextView中的一些网址。或者我可以使用NSAttributedString的NSLinkAttributeName属性手动添加链接。
I can create a hyperlink to some url in an NSTextView using the "Link Panel". Or I can add a link manually using the NSLinkAttributeName attribute of NSAttributedString.
我不想制作一个超链接到一些外部url,但我想能够创建一个超链接到NSTextView中的一些文本。你知道如何在页面中你可以设置一些文本作为书签,然后你可以做一个超链接到那个书签?
I don't want to make a hyperlink to some external url though, I want to be able to create a hyperlink to some text within the NSTextView. Do you know how in Pages you can set some text as a Bookmark, and then you can make a hyperlink to that bookmark? Any ideas or examples of how to go about that?
推荐答案
我没有这样做,但是它像下面这样:
I haven't done this in a while so take this with a grain of salt but it goes something like:
- 您需要为应用程序定义一个协议来处理URL。它将类似于myApplicationName:// aPath
- 在应用程序
Info.plist
文件中添加CFBundleURLTypes
键。 - 编写应用程序代码以响应来自NSWorkspace的
openURL
。这意味着应用程序必须了解如何将URL样式路径转换为其文档中的特定位置。
- You need to define a protocol for you app to handle URLs. It will look something like "myApplicationName://aPath"
- In the apps
Info.plist
file add the protocol underCFBundleURLTypes
key. - Write code for the app to respond to
openURL
from NSWorkspace. This means the app will have to understand how to convert a URL style path to a specific location in one of its documents.
我认为处理URL路径的最佳方法是为每个文档分配一个UUID,然后为段落分配一个数字方案,句子。 URL最终如下所示: myApplicationName:// UUID / paragraphNumber / sentenceNumber / wordNumber
。或者,您可以插入隐藏文本以定义锚点,并只搜索该锚点。
I think the best method for handling the URL path is to assign a UUID to each document and then a numeric scheme to the paragraph and sentences. The URL ends up looking like: myApplicationName://UUID/paragraphNumber/sentenceNumber/wordNumber
. Alternatively you can insert hidden text to define an anchor and just search for that anchor.
这篇关于在NSTextView中创建一些文本的超链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!