2010-01-25 39 views
5

我在NSTextField中使用的文本从文件中加载如下。为什么有时超链接不会显示在带有NSAttributedString的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}}.} 

alt text http://pokercopilot.com/stuff/stackoverflow/screenshot2.png

alt text http://pokercopilot.com/stuff/stackoverflow/screenshot1.png

+0

你能向我们展示Credits.rtf吗? – diederikh 2010-01-25 11:44:00

+0

@Diederik,完成。我编辑了问题以包含RTF。 – 2010-01-25 11:52:48

回答

6

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

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

第三种解决方案是使用DSClickableTextField

+0

RTF已经有超链接,问题在于当你点击NSTextField时,超链接只显示蓝色和下划线 – 2010-01-26 15:09:06

+0

这不是我在说的。“**看起来像**链接**在RTF中以这种方式设置它们的样式**:即在RTF中将链接设置为蓝色并加下划线。 – 2010-01-26 16:56:42

+0

Bingo!手动将链接设置为蓝色并用下划线进行设置。 – 2010-01-26 20:39:09

2

确保您设置[creditsLabel setAllowsEditingTextAttributes: YES],否则链接将无法点击。

+0

在界面生成器中,我已经设置了这种情况,并且链接是可点击的。但为了以防万一,我手动添加了该行。它不能解决问题。 :-( – 2010-01-25 17:27:42

相关问题