简体   繁体   English

如何更改NSScroller的颜色?

[英]How to change NSScroller color?

I'm trying to change the color of the scroller itself, it seems I need to overwrite something around drawKnob but I couldn't find any method which seemed appropriate. 我正在尝试更改滚动条本身的颜色,似乎我需要覆盖drawKnob周围的内容,但找不到任何合适的方法。

One way I thought about was to inject a new style and get scroller to use it but again, I couldn't find anything about NSColor in NSScroller header. 我考虑过的一种方法是注入一种新样式并让滚动器使用它,但是再次,我在NSScroller标头中找不到关于NSColor的任何信息。

Any idea? 任何想法?

我认为您将不得不编写一个子类,并重写drawKnob ,也许是drawKnobSlotInRect:也取决于您要更改的内容。

No need to redraw from scratch. 无需从头开始重新绘制。 It's as simple as this: 就这么简单:

import AppKit

class CyanideScroller: NSScroller {
    private var _bkgrColour: NSColor = NSColor.black
    private var _knobColour: NSColor = NSColor.white
    private var isHorizontal = false

    var bkgrColour: NSColor {
        get { return _bkgrColour}
        set { _bkgrColour = newValue}
    }

    var knobColour: NSColor {
        get { return _knobColour}
        set { _knobColour = newValue}
    }

    override var frame: CGRect {
        didSet {
            let size = frame.size
            isHorizontal = size.width > size.height
        }
    }

    override func draw(_ dirtyRect: NSRect) {
        _bkgrColour.setFill()
        dirtyRect.fill()
        self.drawKnob()
    }


    override func drawKnob() {
        _knobColour.setFill()

        let dx, dy: CGFloat
        if isHorizontal {
            dx = 0; dy = 3
        } else {
            dx = 3; dy = 0
        }

        let frame = rect(for: .knob).insetBy(dx: dx, dy: dy)
        NSBezierPath.init(roundedRect: frame, xRadius: 3, yRadius: 3).fill()
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM