Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
General
GeneralPortable MacsHardwareNetworking
Applications
Mac ApplicationsEudoraFirefox / MozillaInternet ExplorerOutlook ExpressMS OfficeEntourageExcelPowerPointWordVirtual PCMedia PlayerOther MS Products
Programming
Mac ProgrammingCodeWarriorPerl
Country Specific
Australian Mac GroupUK Mac Group

Mac Forum / Programming / Mac Programming / November 2006



Tip: Looking for answers? Try searching our database.

How to create a clickable link

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ulrich Hobelmann - 26 Nov 2006 16:42 GMT
Hi, I'm trying to have a text label (i.e. NSTextField) display an URL
that should be clickable.  I found several examples and explanations on
how to do this for a textview.  I even tried to override the field
editor for my textfield, but it seems the field editor's text storage
has no relation whatsoever to the textfield's contents (so I couldn't
set text attributes, which I want).  (Is there a way to fix that?
That'd be the easiest thing.)

Is there any way to set attributes for part of a text field?  If not,
what's the cleanest best-practice way of adding a mouse listener and
maybe an underlined font to a textfield (so I could separate my label
into two labels, one of which would be clickable; ugly but better than
nothing)?  Can I use delegation?  Or do I have to subclass the field,
or the cell?

Another question: what's the typical way of having draggable icons
(such as the icon in a browser's location bar)?  Do people just use a
tiny NSImageView?  Again, this doesn't quite fit into a textfield.
Smells like lots of custom coding again...
matt neuburg - 26 Nov 2006 19:31 GMT
> Is there any way to set attributes for part of a text field

Did you set allowsEditingTextAttributes to YES? m.

Signature

matt neuburg, phd = matt@tidbits.com, http://www.tidbits.com/matt/
Tiger - http://www.takecontrolbooks.com/tiger-customizing.html
AppleScript - http://www.amazon.com/gp/product/0596102119
Read TidBITS! It's free and smart. http://www.tidbits.com

Ulrich Hobelmann - 27 Nov 2006 09:29 GMT
> > Is there any way to set attributes for part of a text field
>
> Did you set allowsEditingTextAttributes to YES? m.

Well, but what method do I use then?  Textfields don't have
addAttributes:range: like a text storage, and as I said, even a
dedicated (non-shared) field editor (textview) doesn't seem to *have*
the field's text storage (the field editor's text storage is empty,
while the textfield displays its content just fine...).
matt neuburg - 27 Nov 2006 18:44 GMT
> > > Is there any way to set attributes for part of a text field
> >
[quoted text clipped - 5 lines]
> the field's text storage (the field editor's text storage is empty,
> while the textfield displays its content just fine...).

An NSTextField is an NSControl, so it has a setAttributedString: method.
I'm not sure what you mean about the field editor's text storage;
perhaps you should show your code. m.

Signature

matt neuburg, phd = matt@tidbits.com, http://www.tidbits.com/matt/
Tiger - http://www.takecontrolbooks.com/tiger-customizing.html
AppleScript - http://www.amazon.com/gp/product/0596102119
Read TidBITS! It's free and smart. http://www.tidbits.com

Ulrich Hobelmann - 28 Nov 2006 12:58 GMT
> An NSTextField is an NSControl, so it has a setAttributedString: method.
> I'm not sure what you mean about the field editor's text storage;
> perhaps you should show your code. m.

Thanks a lot, that works like a charm!

The textfield even interprets my LinkAttribute correctly.  The only
thing funny is that when I click the link, the textfield's font changes
to a smaller font...  Don't know why that's happening...
matt neuburg - 28 Nov 2006 16:30 GMT
> > An NSTextField is an NSControl, so it has a setAttributedString: method.
> > I'm not sure what you mean about the field editor's text storage;
[quoted text clipped - 5 lines]
> thing funny is that when I click the link, the textfield's font changes
> to a smaller font...  Don't know why that's happening...

You still are not showing any code so it's impossible to guess. m.

Signature

matt neuburg, phd = matt@tidbits.com, http://www.tidbits.com/matt/
Tiger - http://www.takecontrolbooks.com/tiger-customizing.html
AppleScript - http://www.amazon.com/gp/product/0596102119
Read TidBITS! It's free and smart. http://www.tidbits.com

Ulrich Hobelmann - 28 Nov 2006 20:06 GMT
> > The textfield even interprets my LinkAttribute correctly.  The only
> > thing funny is that when I click the link, the textfield's font changes
> > to a smaller font...  Don't know why that's happening...
>
> You still are not showing any code so it's impossible to guess. m.

Well, the code is boring, and doesn't really do much...  That's why I'm
wondering at the behavior.

[textfield setAllowsEditingTextAttributes:YES];
NSString *url = @"http://someurl/";
NSString *s = [NSString stringWithFormat:@"Look at: %@", url];
NSMutableAttributedString *str =
    [[[NSMutableAttributedString alloc] initWithString:s] autorelease];
NSRange urlRange = [s rangeOfString:@"http://"];
urlRange.length = [s length] - urlRange.location;
NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:
    [NSURL URLWithString:url], NSLinkAttributeName,
    [NSNumber
numberWithInt:NSSingleUnderlineStyle],NSUnderlineStyleAttributeName,
    [NSColor blueColor], NSForegroundColorAttributeName, NULL];
[str beginEditing];
[str addAttributes:attrs range:urlRange];
[str endEditing];
[textfield setAttributedStringValue:str];
matt neuburg - 28 Nov 2006 22:33 GMT
> > > The textfield even interprets my LinkAttribute correctly.  The only
> > > thing funny is that when I click the link, the textfield's font changes
[quoted text clipped - 21 lines]
> [str endEditing];
> [textfield setAttributedStringValue:str];

Ah, I see. The problem is that this is an editable text field but at the
time you are issuing this command it is not actually being edited. (Try
it. You will see that if you are actually editing the field at the time
you issue this command, it starts out with the smaller font.) It is not
changing because you are clicking the link; it is changing because you
are starting to edit the field. (Try it: click in "Look at", not at the
link, and you will see the font become smaller.)

The reason is that you are not supplying any font and size information,
so the font and size for the text field and the field editor are in
conflict. Try this:

NSMutableAttributedString *str =
 [[[NSMutableAttributedString alloc] initWithString:s
 attributes:
   [NSDictionary dictionaryWithObject:
     [NSFont fontWithName:@"Lucida Grande" size:13]
  forKey: NSFontAttributeName]] autorelease];

Problem solved. m.

Signature

matt neuburg, phd = matt@tidbits.com, http://www.tidbits.com/matt/
Tiger - http://www.takecontrolbooks.com/tiger-customizing.html
AppleScript - http://www.amazon.com/gp/product/0596102119
Read TidBITS! It's free and smart. http://www.tidbits.com

Ulrich Hobelmann - 29 Nov 2006 09:19 GMT
> The reason is that you are not supplying any font and size information,
> so the font and size for the text field and the field editor are in
> conflict. Try this:

Oh, I didn't know the field editor installs its own font attributes...

Thanks a lot, Matt!

(I have another related question though: because I also want my
textfield to be selectable, when the user clicks in it, the cursor is
displayed (non-blinking).  I think this shouldn't happen, but I don't
know how to turn it off.)
Ulrich Hobelmann - 28 Nov 2006 20:06 GMT
> > The textfield even interprets my LinkAttribute correctly.  The only
> > thing funny is that when I click the link, the textfield's font changes
> > to a smaller font...  Don't know why that's happening...
>
> You still are not showing any code so it's impossible to guess. m.

Well, the code is boring, and doesn't really do much...  That's why I'm
wondering at the behavior.

[textfield setAllowsEditingTextAttributes:YES];
NSString *url = @"http://someurl/";
NSString *s = [NSString stringWithFormat:@"Look at: %@", url];
NSMutableAttributedString *str =
    [[[NSMutableAttributedString alloc] initWithString:s] autorelease];
NSRange urlRange = [s rangeOfString:@"http://"];
urlRange.length = [s length] - urlRange.location;
NSDictionary *attrs = [NSDictionary dictionaryWithObjectsAndKeys:
    [NSURL URLWithString:url], NSLinkAttributeName,
    [NSNumber
numberWithInt:NSSingleUnderlineStyle],NSUnderlineStyleAttributeName,
    [NSColor blueColor], NSForegroundColorAttributeName, NULL];
[str beginEditing];
[str addAttributes:attrs range:urlRange];
[str endEditing];
[textfield setAttributedStringValue:str];
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.