On 05/11/2008, at 9:42 PM, Nathan Kinsinger wrote:

On Nov 3, 2008, at 1:25 PM, development2 wrote:

I need to create Hyperlink to the companies web site, but the link will be in a NSTextField. How do I create a hyperlink in this text field. I am sure it is something with NSMutableAttributedString but can't seem to figure it out. I though maybe there was some Cocoa class to do that, but could not find anything. Does anyone have one or know where I might be able to find one?

Thanks in advance.

I've done it by creating an attributed string using initWithHTML:documentAttributes: and bound it to the NSTextField. The field should be both selectable and allows rich text. The one drawback is no hand cursor when the mouse hovers over it.

You can get the pointing hand cursor by subclassing the window's content view (the window holding the text field, that is) and override -resetCursorRects like this:

#import "AboutBoxContentView.h"
@implementation AboutBoxContentView
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame]; if (self) {/*Initialization code here.*/} return self;
}
- (void)drawRect:(NSRect)rect {/* Drawing code here.*/}
// The only thing to override here, the following ensures that I get the pointing hand cursor on the mailto link.
- (void)resetCursorRects
{
        NSCursor *pointy = [NSCursor pointingHandCursor];
[self addCursorRect:NSMakeRect(180.0, 31.0, 36.0, 17.0) cursor:pointy];// rect of mailto link, hardcode is alright
        [pointy setOnMouseEntered:YES];
}
@end

NSDictionary *attrsDictionary = [[NSDictionary alloc] initWithObjectsAndKeys: [NSFont labelFontOfSize:[NSFont labelFontSize]], NSFontAttributeName, nil];

NSMutableAttributedString *htmlLinkString = [[NSMutableAttributedString alloc] initWithHTML:[[NSString stringWithFormat:@"<a href=\"[EMAIL PROTECTED]">%@</a>", self.sourceProjectURL, self.sourceProjectURL] dataUsingEncoding: NSUTF8StringEncoding] documentAttributes: nil];

[htmlLinkString addAttributes:attrsDictionary range:NSMakeRange(0, [htmlLinkString length])];


--Nathan
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to