使用属性字符串突出显示 NSStatusItem
我有一个 NSStatusItem,并且为其使用属性字符串,设置如下:
[statusItem setAttributedTitle:as];
其中 as
是我的属性字符串。当满足某些条件时,我用它通过不同的颜色来突出显示项目的某些部分。例如,我的状态项可以有一些红色文本和一些黑色文本。
现在的问题是,当我使用 setAttributedTitle 然后单击状态项时,颜色不会按照我想要的方式反转。例如,当我只使用 setTitle
时,未选择时文本为黑色,选择时变为白色。现在它只保留我设置的颜色。
有没有办法告诉它在选择时反转颜色?如果没有,我怎样才能实现这一目标?抱歉,我是 Objective-C 的初学者。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来执行此操作的唯一方法是:
setMenu:
不要为
statusItem
设置菜单:而是使用
setAction:
,更改颜色字符串的,显示菜单,然后更改颜色back例如,使用类似以下内容:
并实现
statusItemClicked< /code> 方法如下:
Looks like the only way to do this is:
do not set a menu for the
statusItem
usingsetMenu:
instead, use
setAction:
, change the color of the string, show the menu, and then change the color backFor instance, use something like:
And implement the
statusItemClicked
method like this:您可以实现以下 NSMenuDelegate 方法:
you can implement the following NSMenuDelegate methods: