如何更改NSSlider刻度线颜色?

时间:2018-06-03 08:17:43

标签: macos nsslider

我知道应该更改函数drawTickMarks,但我应该怎么做才能更改tickmarks颜色?

1 个答案:

答案 0 :(得分:1)

如果您想要它们的颜色不同,请用不同的颜色绘制它们。

drawTickMarks()的空实现没有任何吸引力。

enter image description here

子类NSSliderCell并设置单元格类。

然后在drawTickMarks()

中提供绘图程序
class CustomSliderCell: NSSliderCell {

    override func drawTickMarks() {

        guard let slider = controlView as? NSSlider, slider.numberOfTickMarks > 0 else {
            return
        }

        let tickWidth: CGFloat = 5
        let horizontalinset: CGFloat = 5
        let verticalinset: CGFloat = 1
        let drawRect = slider.bounds.insetBy(dx: horizontalinset, dy: verticalinset) //squish in slightly
        let step = drawRect.width/CGFloat(numberOfTickMarks-1) //classic fenceposts problem
        var mark = CGFloat(0)
        for _ in 0..<numberOfTickMarks {
            let blob = NSBezierPath(roundedRect: NSRect(x: mark + horizontalinset , y:verticalinset, width: tickWidth, height: tickWidth*2), xRadius: tickWidth/2.0, yRadius: tickWidth/2.0)
            NSColor.green.set()
            blob.fill()
            NSColor.darkGray.set()
            blob.stroke()
            mark += step
        }
    }

}

这是一个演示,远非完美,但应该给你一个需要的想法。

enter image description here