Re: Mail address
Re: Mail address
- Subject: Re: Mail address
- From: email@hidden
- Date: Mon, 4 Mar 2002 14:35:24 -0800
The tools conspire to make this difficult. I've logged various bugs
on various roadblocks to doing this easily in IB. Until they're fixed,
I've written a hack category on NSTextField that turns the textfield
into an NSTextView, scans the string for the relevant text, and adds the
relevant link attribute to that range. It may have issues, no
guarantees, but I use it in my apps. If you need to set the link
attribute on more than one range in a given textfield, you'll need to
extend this code; since it effectively changes the receiver into an
NSTextView, calling this method twice in a row won't work. :->
This is exactly the sort of disgusting hack one should avoid doing at
all costs. You have been warned. As for why it scans backwards for the
relevant string, that's because I needed it to do that. :->
Feel free to e-mail me improvements or changes to this code.
@interface NSTextField (CocoaExtra)
- (void)fixText:(NSString *)text toGoToLink:(NSString *)url;
@end
@implementation NSTextField (CocoaExtra)
// This is the best solution I've found so far to the problem of how to
get clickable links into a window using
// NSLinkAttribute. This circumvents three different problems. One is
that standalone textviews created in IB
// have a black background that is difficult or impossible to get rid
of. Another is that somebody along the
// way strips off link attributes when you copy text and paste it into
IB, so even if you get a proper string
// with the link attribute set up, you can't get it into a textview in
IB; the attribute is lost. Finally,
// IB is really not very happy about textviews that are not contained by
scrollviews anyway, so it makes life
// in IB easier to avoid creating standalone textviews. This solution
is gross, but it works.
- (void)fixText:(NSString *)text toGoToLink:(NSString *)url
{
NSString *tfString = [self stringValue];
NSRange urlRange = [tfString rangeOfString:text
options:NSBackwardsSearch];
int length = [tfString length];
NSMutableParagraphStyle *pStyle = [[[NSParagraphStyle
defaultParagraphStyle] mutableCopy] autorelease];
NSTextView *tv;
NSTextStorage *ts;
[pStyle setAlignment:[self alignment]];
tv = [[NSTextView alloc] initWithFrame:[self frame]];
[tv setString:tfString];
[tv setEditable:NO];
[tv setSelectable:YES];
[tv setRichText:YES];
[tv setDrawsBackground:NO];
[tv setTextContainerInset:NSMakeSize(0, 0)];
[[tv textContainer] setLineFragmentPadding:1.0];
ts = [tv textStorage];
[ts beginEditing];
[ts addAttribute:NSFontAttributeName value:[self font]
range:NSMakeRange(0, length)];
[ts addAttribute:NSParagraphStyleAttributeName value:pStyle
range:NSMakeRange(0, length)];
if ((urlRange.location != 0) && (urlRange.location != NSNotFound))
{
[ts addAttribute:NSLinkAttributeName value:[NSURL
URLWithString:url] range:urlRange];
[ts addAttribute:NSForegroundColorAttributeName value:[NSColor
blueColor] range:urlRange];
[ts addAttribute:NSUnderlineStyleAttributeName value:[NSNumber
numberWithInt:NSSingleUnderlineStyle] range:urlRange];
}
[ts endEditing];
[[self superview] addSubview:tv];
[tv release];
[self removeFromSuperview];
}
@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.