cocoa - 如何在绘制 NSPopUpButton 单元格时手动突出显示它(使用白色而不是黑色绘制)?

标签 cocoa custom-controls nspopupbuttoncell

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

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

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

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

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

有人能给我指出解决这个问题的正确方向吗?

最佳答案

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

-setHighlighted:YES 不是您要找的。来自文档:

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.

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

- (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 中托管此复合单元格,那么这应该足以为所选行获取正确的背景。

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

关于cocoa - 如何在绘制 NSPopUpButton 单元格时手动突出显示它(使用白色而不是黑色绘制)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1162414/

相关文章:

cocoa - NSView 是否有类似于 UIView 的 setNeedsLayout/layoutSubviews 方法?

swift - 以编程方式添加到 NSTableView 的列在 Delegate 中无法识别

c++ - CToolbar 自定义对话框中图标的自定义绘制

java - 创建我自己的自定义标签时出现问题(JSF 2.0)

cocoa - 使用字符串值填充 NSPopUpButtonCell

objective-c - NSPopupButton 内容绑定(bind)到 NSAttributedString

iphone - pushViewController 不工作,即使我有一个 navigationController

c# - 核心数据与 LINQ to SQL?

android - startActivity 在自定义控件(RadioButton)内不起作用

objective-c - NSOutlineView 中动态填充的 NSPopUpButtonCell 菜单