Pablo
Pablo

Reputation: 29519

How to specify alpha for NSButtonCell? (for disabled state)

I am overriding drawBezel to draw button with specific shape. Now what I want is to set cell's alpha to 50% when it is disabled. I know I have to use isEnabled, but how to specify alpha for whole cell, including title?

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()
        }
    }
}

Upvotes: 1

Views: 228

Answers (1)

ekscrypto
ekscrypto

Reputation: 3816

Use:

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

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

Upvotes: 1

Related Questions