8

I'm following THIS guide from apple, but it's not really working properly.

Basically, I'm trying to add a hyperlink to an NSTextField within a Window, via a custom WindowController class. I'm able to get the hyperlink working with a few problems:

  • When I hover over the hyperlink I get an 'I bean' (the cursor that indicates you can select text). I want a hand that normally appears over hyperlinks
  • When I click on the hyperlinked text, it opens up the link in my browser successfully, but then it changes the text size and format (e.g. it is no longer centered, back to some default). Now when I hover over it I get the hand, though.

After a bit of experimentation, I've discerned that the initial string formatting (e.g. size, font before I click on it) is that of the .xib file I created the label in. After I click, it changes to some default font that I can't seem to affect in any way. The hyperlink persists, though.

Here's some of the code:

AboutWindowController.h

#import "AboutWindowController.h"

@interface AboutWindowController ()

@end

@implementation AboutWindowController

- (id)initWithWindow:(NSWindow *)window
{
    self = [super initWithWindow:window];
    if (self) {
        // Initialization code here.
    }
    return self;
}

- (void)windowDidLoad
{
    [super windowDidLoad];

    [self.testLabel setAllowsEditingTextAttributes:YES];
    [self.testLabel setSelectable:YES];

    NSMutableAttributedString* string1 = [[NSMutableAttributedString alloc] init];

    NSString* inString = @"Apple Computer";
    NSURL* aURL = [NSURL URLWithString:@"www.google.com"];

    NSMutableAttributedString* attrString = [[NSMutableAttributedString alloc] initWithString: inString];
    NSRange range = NSMakeRange(0, [attrString length]);

    [attrString beginEditing];
    [attrString addAttribute:NSLinkAttributeName value:[aURL absoluteString] range:range];

    // make the text appear in blue
    [attrString addAttribute:NSForegroundColorAttributeName value:[NSColor blueColor] range:range];

    // next make the text appear with an underline
    [attrString addAttribute:
     NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:NSSingleUnderlineStyle] range:range];

    [attrString endEditing];


    [string1 appendAttributedString:  attrString];


    [self.testLabel setAttributedStringValue:string1];
    [self.testLabel setFont:[NSFont fontWithName:@"Helvetica" size:20]];
}
@end

AboutWindowController.h

#import <Cocoa/Cocoa.h>

@interface AboutWindowController : NSWindowController
@property (weak) IBOutlet NSTextField *testLabel;

@end

The .xib is very simple: it's a window with a label in it, and I linked the label to the .h file properly (I believe)

Thanks for the help. I'll try to check back regularly to answer any questions/clarifications. EDIT: Please check my comment on bikram's answer for an update of my situation.

Connor
  • 533
  • 4
  • 12

2 Answers2

2

The problem you are hitting is that NSMutableAttributedString is trying to force its formatting and NSTextField is forcing its own.

My main XIB is having only menu and my windowController XIB has one Label NSTextField.

windowController:

@interface TFTWindowController : NSWindowController

@property (weak) IBOutlet NSTextField *testLabel;

@end

@implementation TFTWindowController

- (id)initWithWindow:(NSWindow *)window
{
    self = [super initWithWindow:window];
    if (self) {
        // Initialization code here.
    }
    return self;
}

- (void)awakeFromNib {

}

- (void)windowDidLoad
{
    [super windowDidLoad];
    [self.testLabel setAllowsEditingTextAttributes:YES];
    [self.testLabel setSelectable:YES];

    NSMutableAttributedString* string1 = [[NSMutableAttributedString alloc] init];

    NSString* inString = @"Apple Computer";
    NSURL* aURL = [NSURL URLWithString:@"www.google.com"];

    NSMutableAttributedString* attrString = [[NSMutableAttributedString alloc] initWithString:inString];
    NSRange range = NSMakeRange(0, [attrString length]);

    [attrString beginEditing];
    [attrString addAttribute:NSLinkAttributeName value:[aURL absoluteString] range:range];

    // make the text appear in blue
    [attrString addAttribute:NSForegroundColorAttributeName value:[NSColor blueColor] range:range];

    // next make the text appear with an underline
    [attrString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:NSSingleUnderlineStyle] range:range];
    [attrString addAttribute:NSFontAttributeName value:[NSFont fontWithName:@"Helvetica" size:20] range:range];

    [attrString endEditing];


    [string1 appendAttributedString:  attrString];


    [self.testLabel setAttributedStringValue:string1];
    [self.testLabel setFont:[NSFont fontWithName:@"Helvetica" size:20]];
}

@end

AppDelegate:

@interface TFTAppDelegate : NSObject <NSApplicationDelegate>

@property(nonatomic, strong)TFTWindowController *windowController;

@end

@implementation TFTAppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Insert code here to initialize your application
    self.windowController = [[TFTWindowController alloc] initWithWindowNibName:@"TFTWindowController"];
    [_windowController showWindow:nil];
}
bikram990
  • 1,085
  • 1
  • 14
  • 36
  • Thanks for the answer -- however, it did not fix everything. When I hover over the label (Apple Computer, size 20 helvetica), it still doesn't show a white hang cursor (for clicking links); just an I bar. Furthermore, when I comment out `[self.testLabel setFont ...]` AND `[attrString addAttribute:NSFontAttributeName ...]`, on clicking the label it still resets the text's font. Perhaps that provides some insight? – Connor Jun 20 '14 at 18:06
  • You might be doing something else also as in the test project i've just added this function and i does show the hand cursor and it doesn't switch the font back if you want the test project i can post it here. – bikram990 Jun 21 '14 at 11:11
  • That would be very useful, please do! – Connor Jun 21 '14 at 19:11
  • BTW, I'm using `XCode 5.1.1` on `Mavericks 10.9.3`. Please Upgrade the OS if you are using `10.9.2` as i've seen some UI issues unrelated to this on that. – bikram990 Jun 22 '14 at 05:39
  • I am using 5.1.1 and 10.9.3 for sure. Somehow after using your code, I'm still experiencing problems. This is confusing. I've copy-pasted my code here (which I believe I copied exactly from your post - sans things like setting up the header files). http://pastebin.com/gxMAPEk4 – Connor Jun 23 '14 at 19:59
  • @Vare the font is not changing for me but however the cursor problem is there. But that too vanishes if i click in the text field. – bikram990 Jun 24 '14 at 04:32
  • Likewise. I'm new to stack overflow - is this a time when I would make a new thread titled something like "Attributed string hyperlink not showing proper cursor"? – Connor Jun 24 '14 at 17:21
  • BTW i just found http://toomasvahter.wordpress.com/2010/06/06/embedding-hyperlinks-in-nstextview/ – bikram990 Jun 25 '14 at 04:32
0

It seems you need to focus the textfield to make the hand shown. I suggest adding these two lines to the code to solve the cursor problem:

[self.testLabel selectText:self];
[[self.testLabel currentEditor] setSelectedRange:NSMakeRange(0, 0)];

In this answer, I use some code in this link.

Tai Ly
  • 311
  • 1
  • 7