• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Clickable text sample code
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Clickable text sample code


  • Subject: Clickable text sample code
  • From: Ben Haller <email@hidden>
  • Date: Tue, 15 Jul 2003 13:15:15 -0700

Somebody contacted me recently by email asking how to make a "clickable text" control, since I had posted about NSTextView's built-in link-following facilities before. Well, NSTextView's facilities aren't great, mostly because the link doesn't change color when you click on it or after it has been visited, and also because NSTextView is a complicated enough object that it's a bit tricky to set everything up properly. One really wants a nice, lightweight control that does the right thing very easily and seamlessly. So I hacked together this sample code, and figured I'd post it here; I must be in a helpful mood today. :->
It's a subclass of NSTextField, in order to get a bunch of stuff (most notably the display and wrapping of the text) for free. It doesn't know anything about links or URLs as such, it just fires its action; it could be easily modified to keep an NSURL and call NSWorkspace to open the URL when clicked, of course. It seems to work in my app, your milage may vary. :-> If anybody makes any useful modifications to this, please let me know.

---------------------------------------------------------------------

//
// ClickableText.h
//
// Created by Ben Haller on Tue Jul 15 2003.
//
// This code is hereby released into the public domain. Do with it as you wish.
//

#import <AppKit/AppKit.h>


@interface NSColor (ClickableTextColors)

+ (NSColor *)basicClickableTextColor;
+ (NSColor *)trackingClickableTextColor;
+ (NSColor *)visitedClickableTextColor;

@end

@interface ClickableText : NSTextField
{
BOOL beingClicked, beenClicked;
}

- (id)initWithFrame:(NSRect)frame;

@end

---------------------------------------------------------------------

//
// ClickableText.m
//
// Created by Ben Haller on Tue Jul 15 2003.
//
// This code is hereby released into the public domain. Do with it as you wish.
//

#import "ClickableText.h"


@implementation NSColor (ClickableTextColors)

+ (NSColor *)basicClickableTextColor
{
static NSColor *cachedColor = nil;

if (!cachedColor)
cachedColor = [[NSColor colorWithCalibratedRed:0.0 green:0.0 blue:1.0 alpha:1.0] retain];

return cachedColor;
}

+ (NSColor *)trackingClickableTextColor
{
static NSColor *cachedColor = nil;

if (!cachedColor)
cachedColor = [[NSColor colorWithCalibratedRed:1.0 green:0.0 blue:0.0 alpha:1.0] retain];

return cachedColor;
}

+ (NSColor *)visitedClickableTextColor
{
static NSColor *cachedColor = nil;

if (!cachedColor)
cachedColor = [[NSColor colorWithCalibratedRed:0.4 green:0.2 blue:0.7 alpha:1.0] retain];

return cachedColor;
}

@end

@implementation ClickableText

- (void)finishInitialization
{
[self setBordered:NO];
[self setBezeled:NO];
[self setDrawsBackground:NO];
[self setEditable:NO];
[self setSelectable:NO];
[self setEnabled:YES];
[self setTextColor:[NSColor basicClickableTextColor]];
}

- (id)initWithCoder:(NSCoder *)decoder
{
if (self = [super initWithCoder:decoder])
{
[self finishInitialization];
}

return self;
}

- (id)initWithFrame:(NSRect)frame
{
if (self = [super initWithFrame:frame])
{
[self finishInitialization];
}

return self;
}

- (void)mouseDown:(NSEvent *)event
{
BOOL mouseInside = YES;

beingClicked = YES;
[self setTextColor:[NSColor trackingClickableTextColor]];

while (beingClicked && (event = [[self window] nextEventMatchingMask:(NSLeftMouseUpMask | NSLeftMouseDraggedMask)]))
{
NSEventType type = [event type];
NSPoint location = [event locationInWindow];

location = [self convertPoint:location fromView:nil];
mouseInside = NSPointInRect(location, [self bounds]);

if (mouseInside)
[self setTextColor:[NSColor trackingClickableTextColor]];
else if (beenClicked)
[self setTextColor:[NSColor visitedClickableTextColor]];
else
[self setTextColor:[NSColor basicClickableTextColor]];

if (type == NSLeftMouseUp)
beingClicked = NO;
}

if (mouseInside)
{
beenClicked = YES;
[self setTextColor:[NSColor visitedClickableTextColor]];
[self sendAction:[self action] to:[self target]];
}
}

@end

---------------------------------------------------------------------

Ben Haller
Stick Software
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

  • Follow-Ups:
    • Re: Clickable text sample code
      • From: John Randolph <email@hidden>
  • Prev by Date: Re: System serial number
  • Next by Date: DockIcon with text
  • Previous by thread: Runtime _NSCachedBitmapImageRep class not loaded error
  • Next by thread: Re: Clickable text sample code
  • Index(es):
    • Date
    • Thread