Hyperlink

Hi,

I’m trying to display a hyperlink in a text field.
I got it working, and i get i handcursor when i select a piece of text. But my problem is that I don’t get the handcursor when rolling the mouse over the text field.

So, my problem is a described in this post: http://forums.macrumors.com/showthread.php?t=677713

And the solution is apparently:

If you don’t already have an NSTextField subclass, make one and put this in it:

Code:

  • (void)resetCursorRects
    {
    [self addCursorRect:[self bounds] cursor:[NSCursor pointingHandCursor]];
    }

Can this be done using Applescript Studio?

/Sebastian

I solved it. If anyone want’s to know, here’s how.

First download DSClickableURLTextField and add that to your project by right-clicking on “Other Sources” and “Add → Existing Files”.
Add the following in DSClickableURLTextField.m:

- (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.1 green:0.1 blue:1.0 alpha:1.0],
	   NSForegroundColorAttributeName,
	   nil];
	
	NSAttributedString *attrString = [[[NSAttributedString alloc] initWithString:aStr attributes:attributes] autorelease];
	[self setAttributedStringValue:attrString];
}

Open IB and drag a normal text field from the Library palette to your window. Highlight the text field, open Inspector and go the the Identity view. At the top under "Class Identity change class from NSTextField to DSClickableURLTextField. Then under the AppleScript view check “awake from nib” and enter a name (In this example i named it “hyperlink”).

Back in Xcode add the following under the on awake from nib handler:

if name of theObject is "hyperlink" then
		call method "setStringValue:andURL:" of theObject with parameters {"Go to macscripter.net", "http://www.macscripter.net"}
		return
	end if

Link to DSClickableURLTextField: http://www.nightproductions.net/developer.htm#DSClickableURLTextField

And that’s it!

/Sebastian

Sorry to dig this 2 months old topic but, can I change the alignment of the text? In this case moving the text to the right size of the text field instead of the standard left?

Thanks

try this

[code]- (void)setStringValue:(NSString *)aStr andURL:(NSString *)aURL
{
NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[style setAlignment:NSRightTextAlignment];

NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:NSSingleUnderlineStyle], NSUnderlineStyleAttributeName,
aURL,NSLinkAttributeName,
style, NSParagraphStyleAttributeName,
[NSFont systemFontOfSize:[NSFont smallSystemFontSize]], NSFontAttributeName,
[NSColor colorWithCalibratedRed:0.1 green:0.1 blue:1.0 alpha:1.0],
NSForegroundColorAttributeName,

nil];
[style release];
NSAttributedString *attrString = [[[NSAttributedString alloc] initWithString:aStr attributes:attributes] autorelease];
[self setAttributedStringValue:attrString];
}[/code]

Thanks Stefan! :slight_smile:

I don’t understand obj-c but I can tweak it a little (sometimes) that’s all… Here’s what I went for, it changes the text colour, text size & removes the underlining…

[code]- (void)setStringValue:(NSString *)aStr andURL:(NSString *)aURL
{
NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[style setAlignment:NSRightTextAlignment];

NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
							aURL,NSLinkAttributeName,
							style, NSParagraphStyleAttributeName,
							[NSFont systemFontOfSize:11], NSFontAttributeName,
							[NSColor colorWithCalibratedRed:1.0 green:1.0 blue:1.0 alpha:1.0],
							NSForegroundColorAttributeName,
							nil];
[style release];
NSAttributedString *attrString = [[[NSAttributedString alloc] initWithString:aStr attributes:attributes] autorelease];
[self setAttributedStringValue:attrString];

}[/code]
Works perfectly thanks :slight_smile: