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

发布于 2024-11-26 05:53:23 字数 392 浏览 2 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

贪恋 2024-12-03 05:53:23

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

  • 使用 setMenu:

    不要为 statusItem 设置菜单

  • 而是使用setAction:,更改颜色字符串的,显示菜单,然后更改颜色back

例如,使用类似以下内容:

[statusItem setAction:@selector(statusItemClicked)];

并实现 statusItemClicked< /code> 方法如下:

- (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
}

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
}
对不⑦ 2024-12-03 05:53:23

您可以实现以下 NSMenuDelegate 方法:

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

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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文