用属性字符串突出显示NSStatusItem [英] Highlighting NSStatusItem with attributed string

查看:64
本文介绍了用属性字符串突出显示NSStatusItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个NSStatusItem,并且为其使用了属性字符串,设置如下:

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

[statusItem setAttributedTitle:as];

其中 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.

现在的问题是,当我使用 setAttributedTitle 然后单击状态项时,颜色不会像我想要的那样被反转.例如,当我仅使用 setTitle 时,未选中时文本为黑色,而选中时为白色.现在,它只保留我设置的颜色.

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.

有没有一种方法可以让它在被选中时反转颜色?如果没有,我该如何实现?抱歉,我是Objective-C的初学者.

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.

推荐答案

看起来唯一的方法是:

  • 不要使用 setMenu:

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

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

并实现 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
}

这篇关于用属性字符串突出显示NSStatusItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆