【问题标题】:Why do hyperlinks sometimes not show in an NSTextField with an NSAttributedString?为什么超链接有时不会显示在带有 NSAttributedString 的 NSTextField 中?
【发布时间】:2010-01-25 11:39:47
【问题描述】:

我在 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


(来源:pokercopilot.com

【问题讨论】:

  • 你能给我们看看 Credits.rtf 吗?
  • @Diederik,完成。我编辑了问题以包含 RTF。

标签: cocoa rtf nstextfield nsattributedstring


【解决方案1】:

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

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

第三种解决方案是使用DSClickableTextField

【讨论】:

  • RTF 已经有超链接。问题是当您单击 NSTextField 时,超链接仅显示蓝色和下划线。
  • 我说的不是这个。 “通过在 RTF 中以这种方式设置样式看起来像链接”:即,使链接在 RTF 中变为蓝色并加下划线。
  • 宾果游戏!手动将链接样式设置为蓝色和下划线。
【解决方案2】:

请务必设置[creditsLabel setAllowsEditingTextAttributes: YES],否则链接将无法点击。

【讨论】:

  • 在 Interface Builder 中,我已经将其设置为这种情况,并且链接是可点击的。但以防万一,我手动添加了该行。它不能解决问题。 :-(
猜你喜欢
  • 1970-01-01
  • 2021-02-28
  • 2021-07-14
  • 1970-01-01
  • 2023-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-14
相关资源
最近更新 更多