Hyperlinks within NSTextField?

Is there a way to make certain words of an NSTextField into links?

I found this: http://www.nightproductions.net/references/dsclickableurltextfield_reference.html, but it relies on the ENTIRE field being a link.

I want only part of the field to become a link. Is there an easy way to do this?

One [not so amazing] thought I had was to make a thin WebView, and simply generate the proper code, and insert it into the WebView, making a fake NSTextField. Of course, there’s probably a better way…

Thanks,

SuperScripter

Well, if you use an NSTextView instead of an NSTextField, you have the Smart links option in IB. That will render any link in the text view clickable. But it requires your links to be spelled out correctly, i.e. http://www.macscripter.net/, not www.macscripter.net. Same goes for email addresses, it has to be mailto:someone@macscripter.net.

I’ve used a modified DSClickableURLTextField class quite successfully and can be called quite easily like this:

call method "setStringValue:andURL:" of (text field "emailTF") with parameters {emailAddress, (("mailto:" & emailAddress) as string)}

the NSTextField has the DSClickableURLTextField class set in it’s identity pane in IB.

just add this method to the .m file:

  • (void)setStringValue:(NSString *)aStr andURL:(NSString *)aURL
    {
    NSDictionary *attributes
    = [NSDictionary dictionaryWithObjectsAndKeys:
    [NSNumber numberWithInt:NSSingleUnderlineStyle], NSUnderlineStyleAttributeName,
    aURL,NSLinkAttributeName,
    //[NSFont systemFontOfSize:[NSFont smallSystemFontSize]], NSFontAttributeName,
    [NSColor colorWithCalibratedRed:0.7 green:0.7 blue:0.7 alpha:1.0],
    NSForegroundColorAttributeName,
    nil];

    NSAttributedString *attrString = [[[NSAttributedString alloc] initWithString:aStr attributes:attributes] autorelease];
    [self setAttributedStringValue:attrString];
    }

BTW, i’ve commented the line about the font styling thing, wanted to preserve the look I established in the app. and you can also change the color of the link with the NSColor line.

Model: MacBookPro2,2
Browser: Safari 531.9
Operating System: Mac OS X (10.6)