簡體   English   中英

如何繪制 NSPopupButtonCell 突出顯示的圖像

[英]How to draw NSPopupButtonCell highlighted image

我在NSToolbar中有 NSPopupButton ,它的borderder = false突出顯示時有一些顏色混合。 如果我使用bordered = true ,則圖像是用漂亮的深色疊加繪制的。

我正在嘗試以與bordered bordered=true相同的方式繪制突出顯示的state

PS:帶有bordered = false的NSButtonCell 開箱即用。

我可以通過使用bordered 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 )

在此處輸入圖像描述

我發現的唯一技巧是繪制一個替代單元格。 仍在尋求更好的解決方案。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM