为什么超链接有时不显示在带有 NSAttributedString 的 NSTextField 中?

发布于 2024-08-18 21:40:09 字数 2083 浏览 7 评论 0原文

我在 NSTextField 中使用的文本是从文件加载的,如下所示。

NSString *path = [[NSBundle mainBundle] pathForResource:@"Credits"  ofType:@"rtf"];
NSAttributedString *as = [[NSAttributedString alloc] initWithPath:path documentAttributes:NULL];
[creditsLabel setAttributedStringValue:as];
[creditsLabel becomeFirstResponder];

窗口中的超链接不会以蓝色下划线呈现,除非我首先单击 NSTextField 上的某个位置(如两个屏幕截图所示)。

如何使这些超链接始终看起来像超链接?

这是 RTF:

{\rtf1\ansi\ansicpg1252\cocoartf1038\cocoasubrtf250
{\fonttbl\f0\fnil\fcharset0 LucidaGrande;}
{\colortbl;\red255\green255\blue255;}
\pard\tx560\tx1120\tx1680\tx2240\tx2800\tx3360\tx3920\tx4480\tx5040\tx5600\tx6160\tx6720\ql\qnatural\pardirnatural

\f0\fs24 \cf0 Copyright \'a9 2009-2010 Fully Functional Software.\
All rights reserved.\
\
BlazingStars is made possible by {\field{\*\fldinst{HYPERLINK "http://www.iterasi.net/openviewer.aspx?sqrlitid=p4mjpt7nl02tyjl08_ctaa"}}{\fldrslt DBPrefsWindowController}}, {\field{\*\fldinst{HYPERLINK "http://mattgemmell.com/source"}}{\fldrslt Image Crop}}, {\field{\*\fldinst{HYPERLINK "http://code.google.com/p/tesseract-ocr/"}}{\fldrslt Tesseract-OCR}}, {\field{\*\fldinst{HYPERLINK "http://andymatuschak.org/articles/2005/12/18/help-with-apple-help"}}{\fldrslt Andy's Help Toolkit}}, and {\field{\*\fldinst{HYPERLINK "http://wafflesoftware.net/shortcut/"}}{\fldrslt Shortcut Recorder}}.\
\
Includes icons from the BlueCons set by {\field{\*\fldinst{HYPERLINK "http://www.mouserunner.com"}}{\fldrslt Ken Saunders}}.}

替代文字
(来源:pokercopilot.com

alt text
(来源:
pokercopilot.com

The text I use in an NSTextField is loaded from a file as follows.

NSString *path = [[NSBundle mainBundle] pathForResource:@"Credits"  ofType:@"rtf"];
NSAttributedString *as = [[NSAttributedString alloc] initWithPath:path documentAttributes:NULL];
[creditsLabel setAttributedStringValue:as];
[creditsLabel becomeFirstResponder];

The hyperlinks in the window don't render in blue underline unless I first click somewhere on the NSTextField, as per the two screenshots.

How can I make these hyperlinks always look like hyperlinks?

Here's the RTF:

{\rtf1\ansi\ansicpg1252\cocoartf1038\cocoasubrtf250
{\fonttbl\f0\fnil\fcharset0 LucidaGrande;}
{\colortbl;\red255\green255\blue255;}
\pard\tx560\tx1120\tx1680\tx2240\tx2800\tx3360\tx3920\tx4480\tx5040\tx5600\tx6160\tx6720\ql\qnatural\pardirnatural

\f0\fs24 \cf0 Copyright \'a9 2009-2010 Fully Functional Software.\
All rights reserved.\
\
BlazingStars is made possible by {\field{\*\fldinst{HYPERLINK "http://www.iterasi.net/openviewer.aspx?sqrlitid=p4mjpt7nl02tyjl08_ctaa"}}{\fldrslt DBPrefsWindowController}}, {\field{\*\fldinst{HYPERLINK "http://mattgemmell.com/source"}}{\fldrslt Image Crop}}, {\field{\*\fldinst{HYPERLINK "http://code.google.com/p/tesseract-ocr/"}}{\fldrslt Tesseract-OCR}}, {\field{\*\fldinst{HYPERLINK "http://andymatuschak.org/articles/2005/12/18/help-with-apple-help"}}{\fldrslt Andy's Help Toolkit}}, and {\field{\*\fldinst{HYPERLINK "http://wafflesoftware.net/shortcut/"}}{\fldrslt Shortcut Recorder}}.\
\
Includes icons from the BlueCons set by {\field{\*\fldinst{HYPERLINK "http://www.mouserunner.com"}}{\fldrslt Ken Saunders}}.}

alt text
(source: pokercopilot.com)

alt text
(source: pokercopilot.com)

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

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

发布评论

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

评论(2

你不是我要的菜∠ 2024-08-25 21:40:09

您可以通过在 RTF 中设置链接样式来使链接看起来像链接,但文本字段不会处理点击,除非您启用编辑文本属性并选择文本。 (这两种说法的来源:QA1487。)我在你的对 Diederik Hoogenboom 的回答进行评论,您已经完成了最后一部分,因此您现在需要做的就是在整个 RTF 中撒上蓝色和下划线。

另一种解决方案是使用文本视图而不是文本字段。

第三种解决方案是使用 DSClickableTextField

You can get the links to look like links by styling them that way in the RTF, but the text field won't handle clicks unless you enable editing text attributes and selecting text. (Source for both claims: QA1487.) I see in your comment on Diederik Hoogenboom's answer that you've already done the last part, so all you need to do now is sprinkle blue and underline throughout the RTF.

Another solution would be to use a text view instead of a text field.

A third solution would be to use DSClickableTextField.

苦妄 2024-08-25 21:40:09

确保设置[creditsLabel setAllowsEditingTextAttributes: YES],否则链接将不可点击。

Make sure you set [creditsLabel setAllowsEditingTextAttributes: YES], otherwise the links will not be clickable.

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