如何在绘制 NSPopUpButtonCell 时手动突出显示它(使用白色而不是黑色绘制)?

发布于 2024-07-27 19:57:19 字数 377 浏览 7 评论 0原文

我有一个由多个单元组成的自定义单元,其中之一是 NSPopUpButtonCell。

当突出显示我的自定义单元格时,我想让所有子单元格也突出显示(通常通过变成白色)。

例如,对于 NSTextCell,如果我在调用 drawWithFrame:inView 之前调用 setHighlighted:YES,则单元格将用白色文本绘制,完全按照我想要的那样。

这不适用于 NSPopUpButtonCells。 文本继续绘制为黑色。

看起来这应该是可能的,因为将 NSPopUpButtonCell 放入 NSTableView 中会正确突出显示。

有人可以指出我解决此问题的正确方向吗?

I've got a custom cell composed out of several cells, one of which is an NSPopUpButtonCell.

When drawing my custom cell highlighted, I want to cause all the sub-cells to highlight as well (typically by turning white).

With, for example an NSTextCell, if I call setHighlighted:YES before calling drawWithFrame:inView the cell will be drawn with white text, exactly as I want it.

This does not work with NSPopUpButtonCells. The text just continues to draw as black.

It seems like this should be possible, since dropping an NSPopUpButtonCell into an NSTableView highlights properly.

Can somebody point me in the right direction for fixing this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

天生の放荡 2024-08-03 19:57:19

您在哪里托管这个自定义+复合 NSCell 子类?

-setHighlighted:YES 不是您要找的。 从文档中:

默认情况下,此方法不执行任何操作。
NSButtonCell 类覆盖了这个
方法来绘制按钮
外观由指定
NSCellLightsByBackground,
NSCellLightsByContents,或
NSCellLightsByGray。

通常,单元格的宿主视图将设置单元格的背景样式,并且单元格将在绘制时使用它来适当地显示自身。 将背景样式从主单元格传播到子单元格。

- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
    NSRect textRect, popUpRect;
    NSDivideRect(cellFrame, &textRect, &popUpRect, NSWidth(cellFrame) / 2, NSMinXEdge);

    /* Draw the text cell (self) */
    [super drawInteriorWithFrame: textRect inView: controlView];

    /* Draw our compound popup cell - create & release every time drawn only in example */
    NSPopUpButtonCell *popUpCell = [[NSPopUpButtonCell alloc] initTextCell: @"popup title"];
    [popUpCell setBordered: NO];
    [popUpCell setBackgroundStyle: [self backgroundStyle]];
    [popUpCell drawWithFrame: popUpRect inView: controlView];
    [popUpCell release];
}

如果您在 NSTableView 中托管此复合单元格,那么这应该足以为所选行获取正确的背景。

如果您以自己的方式托管此内容,则可能需要做额外的工作。 (并且需要提供有关主机环境的其他详细信息,然后我们才能提供建议。)

Where are you hosting this custom+composite NSCell subclass?

-setHighlighted:YES isn't what you are looking for. From the documentation:

By default, this method does nothing.
The NSButtonCell class overrides this
method to draw the button with the
appearance specified by
NSCellLightsByBackground,
NSCellLightsByContents, or
NSCellLightsByGray.

Typically the host view for a cell will set the cell's background style, and the cell will use that at draw time to display itself appropriately. Propagate the background style from the master cell to the sub-cells.

- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
    NSRect textRect, popUpRect;
    NSDivideRect(cellFrame, &textRect, &popUpRect, NSWidth(cellFrame) / 2, NSMinXEdge);

    /* Draw the text cell (self) */
    [super drawInteriorWithFrame: textRect inView: controlView];

    /* Draw our compound popup cell - create & release every time drawn only in example */
    NSPopUpButtonCell *popUpCell = [[NSPopUpButtonCell alloc] initTextCell: @"popup title"];
    [popUpCell setBordered: NO];
    [popUpCell setBackgroundStyle: [self backgroundStyle]];
    [popUpCell drawWithFrame: popUpRect inView: controlView];
    [popUpCell release];
}

If you are hosting this composite cell in an NSTableView, that should be sufficient to get the correct background for selected rows.

If you are hosting this in your own view, you may need to do additional work. (And need to provide additional details about the host environment before we can offer advice.)

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