代码之家  ›  专栏  ›  技术社区  ›  Pablo

如何为NSButtonCell指定alpha?(对于禁用状态)

  •  1
  • Pablo  · 技术社区  · 7 年前

    drawBezel 绘制具有特定形状的按钮。现在我想要的是在禁用时将单元格的alpha设置为50%。我知道我必须用 isEnabled ,但如何为整个单元格(包括标题)指定alpha?

    class FunkyCell: NSButtonCell
    {
        ...
        override func drawTitle(_ title: NSAttributedString, withFrame frame: NSRect, in controlView: NSView) -> NSRect {
            return super.drawTitle(title, withFrame: frame.offsetBy(dx: 0, dy: 0), in: controlView)
        }
    
        func redraw() {
            self.backgroundColor = self.backgroundColor
        }
    
        override func drawBezel(withFrame frame: NSRect, in controlView: NSView) {
            let path = NSBezierPath(bound: frame.insetBy(dx: 2, dy: 2), withCorners: corners, withRadius: CGFloat(NumKeypadConstants.cornerRadius), flip: flipIt)
            path.lineWidth = NumKeypadConstants.borderWidth
            if(isHighlighted)
            {
                let fillColor: NSColor = NumKeypadConstants.buttonHighlightColor
                let strokeColor: NSColor = NumKeypadConstants.buttonBorderColor
                fillColor.setFill()
                strokeColor.setStroke()
                path.fill()
                path.stroke()
            }
            else
            {
                let fillColor: NSColor = NumKeypadConstants.buttonBackgroundColor
                let strokeColor: NSColor = NumKeypadConstants.buttonBorderColor
                fillColor.setFill()
                strokeColor.setStroke()
                path.fill()
                path.stroke()
            }
        }
    }
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   ekscrypto    7 年前

    使用:

    let button = self.controlView as? NSButton
    button?.alphaValue = 0.5
    

    参考: https://developer.apple.com/documentation/appkit/nscell/1535913-controlview