Why do hyperlinks sometimes not show in an NSTextField with an NSAttributedString?
Asked Answered
M

2

5

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)

Massasauga answered 25/1, 2010 at 11:39 Comment(2)
Can you show us the Credits.rtf?Womenfolk
@Diederik, Done. I edited the question to include the RTF.Massasauga
P
7

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.

Pillion answered 26/1, 2010 at 9:33 Comment(3)
The RTF already has the hyperlinks. The problem is that the hyperlinks only show blue and underline when you click on the NSTextField.Massasauga
That's not what I was talking about. “to look like links by styling them that way in the RTF”: i.e., make the links blue and underlined in the RTF.Pillion
Bingo! Manually styling the links as blue and underline did it.Massasauga
W
2

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

Womenfolk answered 25/1, 2010 at 16:57 Comment(1)
In Interface Builder I had already set this to be the case, and the links were clickable. But just in case, I manually added the line. It doesn't fix the problem. :-(Massasauga

© 2022 - 2024 — McMap. All rights reserved.