houbysoft
houbysoft

Reputation: 33392

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.

Upvotes: 7

Views: 1155

Answers (2)

jmoody
jmoody

Reputation: 2480

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];

Upvotes: 2

houbysoft
houbysoft

Reputation: 33392

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
}

Upvotes: 4

Related Questions