Changing color of the NSWindow titlebar

后端 未结 3 1421
再見小時候
再見小時候 2021-01-13 20:34

I am developing a desktop application in which I want to change the color of the title bar of an NSWindow. How exactly can I do this?

3条回答
  •  再見小時候
    2021-01-13 21:25

    NSWindow's content view has a superview, which is an instance of NSThemeFrame. That class is responsible for drawing the title text, the window/toolbar background texture, and it contains subviews for everything else (close button, full screen button, NSDocument icon, etc).

    You can use the Objective-C runtime to replace NSThemeFrame's drawRect: method with your own method, which will call the parent implementation and then perform custom drawing on top of it.

    There is also a private method to find the rect the title is drawn in, and public methods on NSFont to find it's font and font size.

    What I did is set the window background colour to be a solid colour (black) instead of a gradient/texture, then set it to be a "textured" window (which causes the background colour to actually be rendered, otherwise it would not happen), then I draw a black square over the title bar in the area where I know the title has already been drawn, then draw my own title in it's place, with light grey instead of dark grey.

    Source code is here: https://github.com/abhibeckert/Dux/blob/master/Dux/DuxProjectWindow.m (note: it only does a custom title text colour if DUX_DARK_MODE == 1)

    Doing this will probably get your app blocked from the Mac App Store, but it is fairly reliable. Just make sure you test it with every new major version of OS X.

提交回复
热议问题