Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSAttributedString in TextField changing/resetting on click

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.

like image 816
Connor Avatar asked Jun 19 '14 23:06

Connor


1 Answers

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];
}
like image 152
bikram990 Avatar answered Nov 05 '22 01:11

bikram990