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

Cocoa 如何在绘制NSPopUpButtonCell时手动高亮显示它(使用白色而不是黑色绘制)?,cocoa,custom-controls,nspopupbuttoncell,Cocoa,Custom Controls,Nspopupbuttoncell,我有一个自定义单元格,由几个单元格组成,其中一个是NSPopUpButtonCell 在高亮显示自定义单元格时,我希望使所有子单元格也高亮显示(通常通过变白) 例如,对于一个NSTextCell,如果我在调用drawWithFrame:inView之前调用setHighlighted:YES,该单元格将以白色文本绘制,完全符合我的要求 这不适用于NSPopUpButtonCells。文本继续绘制为黑色 这似乎是可能的,因为将NSPopUpButtonCell拖放到NSTableView中会正确高

我有一个自定义单元格,由几个单元格组成,其中一个是NSPopUpButtonCell

在高亮显示自定义单元格时,我希望使所有子单元格也高亮显示(通常通过变白)

例如,对于一个NSTextCell,如果我在调用
drawWithFrame:inView
之前调用
setHighlighted:YES
,该单元格将以白色文本绘制,完全符合我的要求

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

这似乎是可能的,因为将NSPopUpButtonCell拖放到NSTableView中会正确高亮显示


有人能告诉我解决这个问题的正确方向吗?

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

-是的,这不是你想要的。从文件中:

默认情况下,此方法不执行任何操作。 NSButtonCell类重写此 方法使用 指定的外观 NSCellLightsByBackground, NScellLightsByContent,或 nscells-lightsbygray

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

- (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中托管此复合单元格,则应足以获得选定行的正确背景


如果您在自己的视图中托管此文件,则可能需要执行其他工作。(在我们提供建议之前,需要提供有关主机环境的其他详细信息。)

您在哪里托管此自定义+复合NSCell子类

-是的,这不是你想要的。从文件中:

默认情况下,此方法不执行任何操作。 NSButtonCell类重写此 方法使用 指定的外观 NSCellLightsByBackground, NScellLightsByContent,或 nscells-lightsbygray

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

- (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中托管此复合单元格,则应足以获得选定行的正确背景


如果您在自己的视图中托管此文件,则可能需要执行其他工作。(在我们提供建议之前,需要提供有关主机环境的更多详细信息。)

这正是我需要的。谢谢,吉姆。这正是我需要的。谢谢你,吉姆。