NSTextView 添加 URL 链接到选定的文本?

发布于 2024-11-09 02:57:03 字数 137 浏览 2 评论 0 原文

我有一个 NSTextView。

我只想向 NSTextView 中选定的文本添加一个属性(NSLinkAttributeName)...

您能帮我吗?

谢谢。

I Have an NSTextView.

I just want to add an Attribute (an NSLinkAttributeName) to the selected Text in the NSTextView...

Can You Help me ?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我的影子我的梦 2024-11-16 02:57:03

您想要获取视图的textStorage(基本上是一个可变的属性字符串),然后将NSLinkAttributeName属性添加到所选范围;该属性的值是要链接到的 URL。

[[textView textStorage] addAttribute: NSLinkAttributeName value: url range:[textView selectedRange]];

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.

[[textView textStorage] addAttribute: NSLinkAttributeName value: url range:[textView selectedRange]];
枯寂 2024-11-16 02:57:03

自从我玩 ObjC 以来已经有一段时间了,但这应该可以解决问题。它将用原始内容替换所选文本并附加您的属性。已检查过,但请原谅任何错别字。

NSTextView *textView = ...;
NSDictionary *attributes = ...;

//Get selected text string from TextView (see Text superclass) and append attr link
NSRange selRange = [textView selectedRange];
NSMutableString *changedStr = [[[textView string] substringWithRange:selRange] mutableCopy];
[changedStr appendString:[attributes objectForKey:NSLinkAttributeName]];

//Replace the selected text range in the TextView
[textView replaceCharactersInRange:selRange withString:[NSString stringWithString:changedStr]];

[changedStr release];

查看类定义:

http://开发人员。 apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSText_Class/Reference/Reference.html

  • -replaceCharactersInRange:withString:
  • -selectedRange
  • -scrollRangeToVisible:如果您想立即呈现更改

http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html

  • substringWithRange:

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.

NSTextView *textView = ...;
NSDictionary *attributes = ...;

//Get selected text string from TextView (see Text superclass) and append attr link
NSRange selRange = [textView selectedRange];
NSMutableString *changedStr = [[[textView string] substringWithRange:selRange] mutableCopy];
[changedStr appendString:[attributes objectForKey:NSLinkAttributeName]];

//Replace the selected text range in the TextView
[textView replaceCharactersInRange:selRange withString:[NSString stringWithString:changedStr]];

[changedStr release];

See class defs:

http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSText_Class/Reference/Reference.html

  • -replaceCharactersInRange:withString:
  • -selectedRange
  • -scrollRangeToVisible: if you want to present your change immediately

http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html

  • substringWithRange:
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文