CocoaDev

Edit AllPages

This may be a stupid newbie question but i could not find anything on the net.

How can i insert a NSTextField with a clickable (blue and underlined) hyperlink in it? I just want to have a little text on the bottom of my app with a clickable hyperlink to my website.


You could try an NSButton. Set it as borderless and style the text to look like a link and then have an action that opens the link using NSWorkspace. Or, possibly a bit easier, drag out an NSTextView, resize however you want (turn off scrollers to make it resize smaller), and then copy and paste a link into it from a webpage or from TextEdit.


And with code, something like this

[myTextField setAllowsEditingTextAttributes: YES]; NSMutableAttributedString *mAttr = myTextField attributedStringValue] mutableCopy]; [[NSDictionary *dict = [NSDictionary dictionaryWithObject: @”http://your.link.com/page” forKey: NSLinkAttributeName]; NSRange range = NSMakeRange(0, [mAttr length]; [mAttr addAttributes: dict range: range)]; [myTextField setAttributedStringValue: mAttr];


thanks. i chose the button and it workes fine - just as intended!


You could subclass NSButton like so, then set it as your button’s class in IB. Set the button’s title in IB too, either directly in Attributes, or via bindings. You don’t need to bind the button’s action, since it’s done in the code below.

// HyperlinkButton.h

#import <Cocoa/Cocoa.h> @interface HyperlinkButton : NSButton


// HyperlinkButton.m

#import “HyperlinkButton.h”

@implementation HyperlinkButton

@end

Apple recommend Extending NSAttributedString, worked well for me after adding a line to set the type to 13pt Lucida Grande. http://developer.apple.com/qa/qa2006/qa1487.html