Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Highlighting NSStatusItem with attributed string

I have a NSStatusItem, and I use an attributed string for it, setting is as such:

[statusItem setAttributedTitle:as];

where as is my attributed string. I use it to highlight certain parts of the item when certain conditions are met by coloring them differently. So my status item can have some red text and some black text, for example.

Now the problem is, when I use setAttributedTitle and then click on the status item, the colors don't get inverted as I want them to. For instance, when I used just setTitle, the text is black when not selected and changes to white when selected. Now it just keeps the color I set it to.

Is there a way to tell it to invert colors when it's selected? If not, how can I achieve this? Sorry, I'm a beginner in Objective-C.

like image 837
houbysoft Avatar asked Jul 27 '11 02:07

houbysoft


2 Answers

Looks like the only way to do this is:

  • do not set a menu for the statusItem using setMenu:

  • instead, use setAction:, change the color of the string, show the menu, and then change the color back

For instance, use something like:

[statusItem setAction:@selector(statusItemClicked)];

And implement the statusItemClicked method like this:

- (void) statusItemClicked {

  // change color of attributed string to its highlighted state here

  [statusItem popUpStatusItemMenu:statusItemMenu]; // show the menu
                                                   // which used to be set
                                                   // using setMenu:

  // change color of attributed string back its non-highlighted state here
}
like image 63
houbysoft Avatar answered Oct 24 '22 03:10

houbysoft


you can implement the following NSMenuDelegate methods:

- (void) menuWillOpen:(NSMenu *) aMenu {
  // use an attributed string to set the title to your highlighted color
}


- (void) menuDidClose:(NSMenu *) aMenu {
  // use an attributed string to set the title black
}

[statusItem setMenu:[self menu]];
[[self menu] setDelegate:self];
like image 2
jmoody Avatar answered Oct 24 '22 03:10

jmoody