首页 > 解决方案 > 如何绘制 NSPopupButtonCell 突出显示的图像

问题描述

我在里面有 NSPopupButton NSToolbarborderder = false它在突出显示时有一些颜色混合。如果我使用bordered = true图像是用漂亮的深色叠加绘制的。

我正在尝试以与它相同的方式绘制突出显示的状态bordered=true

PS:NSButtonCellbordered = false开箱即用。

bordered = true我可以通过拥有和覆盖来实现这种行为,drawBezel并且在那里什么都不做,但我想知道

我尝试过的事情:

-

class ToolbarPopUpButtonCell : NSPopUpButtonCell {

  override var isHighlighted: Bool {
      get { return true }
      set { super.isHighlighted = newValue }
  }   

  override func drawImage(withFrame cellFrame: NSRect, in controlView: NSView) {
      super.drawImage(withFrame: cellFrame, in: controlView)
  }


  //used in case bordered = true so we do nothing
  override func drawBezel(withFrame frame: NSRect, in controlView: NSView) {

  }

  //doesn't work
  override var interiorBackgroundStyle: NSView.BackgroundStyle
      {
      return .raised
  }
}

  class ToolbarPopUpButton: NSPopUpButton {

  override func awakeFromNib() {
      cell?.setCellAttribute(.cellLightsByBackground, to: 1)
  }

  override var intrinsicContentSize: NSSize {
      return NSMakeSize(32 + 5, 32)
  }
}

注意右边的图像适用于bordered = false( NSButtonCell)

在此处输入图像描述

标签: macosnspopupbuttonnstoolbarnscellnspopupbuttoncell

解决方案


我发现的唯一技巧是绘制一个替代单元格。仍在寻求更好的解决方案。

- (void)drawImageWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
    NSImage *image = [self image];
    NSButtonCell *swapCell = [[NSButtonCell alloc] initImageCell:image];
    [swapCell setHighlighted:[self isHighlighted]];
    [swapCell drawImage:image withFrame:cellFrame inView:controlView];
}

推荐阅读