NSAttributedString in TextField shift / reset on click

I follow the IT manual from the apple, but it really does not work properly.

Basically, I'm trying to add a hyperlink to an NSTextField in a window through a custom WindowController class. I can make the hyperlink work with several problems:

  • When I hit the hyperlink, I get "I bean" (the cursor indicates that you can select the text). I need a hand that usually appears above hyperlinks
  • When I click on hyperlinks, it opens the link in my browser successfully, but then it changes the size and format of the text (for example, it is no longer centered, but back by default). Now that I am hanging over him, I get a hand.

After a little experiment, I found that the initial formatting of the string (for example, size, font before I click on it) is the .xib file into which I created the shortcut. After I click, it will change to some by default, which I can not influence in any way. However, the hyperlink remains.

Here are 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 

.xib is very simple: this is a window with a label in it, and I linked the label to the .h file correctly (I suppose)

Thanks for the help. I will try to regularly check the answers to any questions / clarifications. EDIT: Please check out my comment on the Bikram response to update my situation.

+6
source share
2 answers

The problem you are facing is that NSMutableAttributedString trying to format the formatting, and NSTextField forces it on its own.

My main XIB only has a menu, and my XCK Controller 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]; } 
+2
source

It seems you need to focus the text box to show your hand. 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 the code for this link .

0
source

Source: https://habr.com/ru/post/971082/


All Articles