memis
memis

Reputation: 400

How to change the selection color of a NSSegmentedControl button

I am trying to find a way to change the color of the selected control button. Is this possible by subclassing the NSSegmentedControl or NSSegmentedCell or any other way? If so, can someone show me the way?

Upvotes: 3

Views: 1840

Answers (4)

Ely
Ely

Reputation: 9131

When using macOS 12.2.2 or newer, you can use the new property selectedSegmentBezelColor, with 'appearances that support it' according the documentation of Apple. Style 'Rounded' for example seems to work, while 'Textured Rounded' not.

segmentedControl.selectedSegmentBezelColor = .systemOrange

Upvotes: 0

brianLikeApple
brianLikeApple

Reputation: 4371

I did it in different way without override but just using "False color" filter. It is not perfect as it somehow change transparent of color a little bit but it is OK for me.

class RLSegmentedControl: NSSegmentedControl {
init() {
    super.init(frame: NSZeroRect)
    addFilter()
}

required init?(coder: NSCoder) {
    super.init(coder: coder)
    addFilter()
}

func addFilter() {
    let colorFilter = CIFilter(name: "CIFalseColor")!
    colorFilter.setDefaults()
    colorFilter.setValue(CIColor(cgColor: NSColor.white.cgColor), forKey: "inputColor0")
    colorFilter.setValue(CIColor(cgColor: NSColor.black.cgColor), forKey: "inputColor1")

//        colorFilter.setValue(CIColor(cgColor: NSColor.yellow.cgColor), forKey: "inputColor0")
//        colorFilter.setValue(CIColor(cgColor: NSColor.black.cgColor), forKey: "inputColor1")

    self.contentFilters = [colorFilter]
}
}

enter image description here

Upvotes: 3

Edson Guido
Edson Guido

Reputation: 43

You can subclass NSSegmentedCell e override the drawSegment method:

override func drawSegment(_ segment: Int, inFrame frame: NSRect, with controlView: NSView) {
    var color: NSColor
    if selectedSegment == segment {
        color = NSColor.red
    } else {
        color = NSColor.white
    }
    color.setFill()
    frame.fill()
    super.drawSegment(segment, inFrame: frame, with: controlView)
}

Upvotes: 1

floschliep
floschliep

Reputation: 533

You need to subclass NSSegmentedCell and overwrite the following method:

- (NSColor *)highlightColorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView

From the documentation:

Returns the color the receiver uses when drawing the selection highlight.

You should not assume that a cell would necessarily want to draw itself with the value returned from selectedControlColor. A cell may wish to draw with different a selection highlight color depending on such things as the key state of its controlView.

Upvotes: 0

Related Questions