NSTextView 添加 URL 链接到选定的文本?
我有一个 NSTextView。
我只想向 NSTextView
中选定的文本添加一个属性(NSLinkAttributeName
)...
您能帮我吗?
谢谢。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我有一个 NSTextView。
我只想向 NSTextView
中选定的文本添加一个属性(NSLinkAttributeName
)...
您能帮我吗?
谢谢。
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(2)
您想要获取视图的textStorage(基本上是一个可变的属性字符串),然后将
NSLinkAttributeName
属性添加到所选范围;该属性的值是要链接到的 URL。You want to get the view's textStorage (which is basically a mutable attributed string), then add the
NSLinkAttributeName
attribute to the selected range; the value of that attribute is the URL to link to.自从我玩 ObjC 以来已经有一段时间了,但这应该可以解决问题。它将用原始内容替换所选文本并附加您的属性。已检查过,但请原谅任何错别字。
查看类定义:
http://开发人员。 apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSText_Class/Reference/Reference.html
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html
Been a while since I played with ObjC but this should do the trick. It replaces the selected text with the original content with your attr appended. Checked through it but please excuse any typos.
See class defs:
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSText_Class/Reference/Reference.html
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html