使用属性字符串突出显示NSStatusItem

hou*_*oft 7 cocoa objective-c nsstatusitem

我有一个NSStatusItem,我使用了一个属性字符串,设置是这样的:

[statusItem setAttributedTitle:as];
Run Code Online (Sandbox Code Playgroud)

as我的属性字符串在哪里.我通过对它们进行不同着色来使用它来突出显示项目的某些部分.因此,我的状态项可以包含一些红色文本和一些黑色文本.

现在的问题是,当我使用setAttributedTitle然后点击状态项时,颜色不会像我希望的那样反转.例如,当我刚使用setTitle时,未选中时文本为黑色,选中时文本更改为白色.现在它只保留我设置的颜色.

有没有办法告诉它在选择时反转颜色?如果没有,我怎么能实现这个目标?对不起,我是Objective-C的初学者.

hou*_*oft 4

看起来执行此操作的唯一方法是:

  • 不要设置菜单statusItem使用setMenu:

  • 相反,使用setAction:,更改字符串的颜色,显示菜单,然后将颜色改回来

例如,使用类似:

[statusItem setAction:@selector(statusItemClicked)];
Run Code Online (Sandbox Code Playgroud)

并实现statusItemClicked这样的方法:

- (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
}
Run Code Online (Sandbox Code Playgroud)