0
votes

I'd like to customize myButton in order to change background color when mouseDown occurs and return to default color when mouseUp is called.

override func mouseDown(theEvent: NSEvent) {
    super.mouseDown(theEvent)
    self.bgColor = NSColor(hex: 0x4A7AA1)
    self.textColor = NSColor.darkGrayColor()
    self.needsDisplay = true
    self.mouseUp(theEvent)
}

override func mouseUp(theEvent: NSEvent) {
    self.textColor = NSColor.whiteColor()
    self.bgColor = NSColor(hex: 0x6AAFE6, alpha: 0.95)
}

I've tried to run this code but while mouse is down all views not redraw. How Can I perform this feature on myButton:NSButton class?

2

2 Answers

0
votes

All you have to do is override drawRect() in the particular view. In drawRect you can check whether the button is highlighted or not.

func drawRect(dirtyRect: NSRect) {
    super.drawRect(dirtyRect)
    if self.highlighted {
         // Do custom drawing.
    }
}

You don't need to detect mouseDown and up for this. drawRect will be invoked automatically

0
votes

I've implemented custom button color highlitening this way:

    override func draw(_ dirtyRect: NSRect) {
    super.draw(dirtyRect);

    setHighlightedColors();
}

private func setHighlightedColors() {
    let textColorDifference: CGFloat = 50/255;

    let textHighlightedColor = textColor == .clear ? textColor : NSColor(red: textColor.redComponent - textColorDifference,
                                                                      green: textColor.greenComponent - textColorDifference,
                                                                      blue: textColor.blueComponent - textColorDifference,
                                                                      alpha: textColor.alphaComponent);


    let backgroundColorDifference: CGFloat = 20/255;
    let backgroundHighlightedColor = backgroundColor == .clear ? backgroundColor : NSColor(red: backgroundColor.redComponent - backgroundColorDifference,
                                                                                  green: backgroundColor.greenComponent - backgroundColorDifference,
                                                                                  blue: backgroundColor.blueComponent - backgroundColorDifference,
                                                                                  alpha: backgroundColor.alphaComponent);

    let borderColorDifference: CGFloat = 50/255;
    let borderHighlightedColor = borderColor == .clear ? borderColor : NSColor(red: borderColor.redComponent - borderColorDifference,
                                                                          green: borderColor.greenComponent - borderColorDifference,
                                                                          blue: borderColor.blueComponent - borderColorDifference,
                                                                          alpha: borderColor.alphaComponent);
    if isHighlighted {
        applyTextColorAndFont(color: textColor, font: customFont);
        applyBackgroundColor(color: backgroundHighlightedColor);
        applyBorder(color: borderHighlightedColor, width: borderWidth)
    } else {
        applyTextColorAndFont(color: textHighlightedColor, font: customFont);
        applyBackgroundColor(color: backgroundColor);
        applyBorder(color: borderColor, width: borderWidth)
    }
}