How to change NSToolBar background color? - objective-c

I'm using a NSToolbar and I'm trying to change the color to a white instead of the default gray. How can I accomplish that?

Since the toolbar integrates into the window title bar, you need to draw both the title bar and the toolbar. Which basically means you need to take over drawing for the entire window.
This requires "swizzling" which, while it works well, is not supported by Apple and therefore may get your app rejected from the app store.
Basically you need to inject your own code into the NSWindow class, by using objective-c runtime functions to modify the class definition. Note that this will affect all windows in your app, so you will need to have an if statement checking if this is a window you want to modify.
Subclass NSWindow, and in the +initialize method, find the "frame view" class which is the one that does most of the window drawing, and rename it's "drawRect:" method to "originalDrawRect:". Then you define a new "drawRect:" method on the class, as a duplicate of a method in your NSWindow subclass. This method should first call originalDrawRect and then do custom drawing on top of it.
Note: you will be drawing over the top of the window title text... so'll need to change the the drawing mode to kCGBlendModeColor or something. Or else just draw the title string again. You can ask the window where the title text should be drawn.
Here's a full article with more details: http://parmanoir.com/Custom_NSThemeFrame

Related

Using multiple windows with Storyboards (Mac OS X development)

I have two window controllers (with their own view controllers) on a storyboard.
In one window, I have the main program, a basic text editor with an NSTextView. In the other window, I have a single button.
I found out how to get the window to display by linking it to a menu item. It works.
The main window is linked to my ViewController class by default. The second window is also linked to the ViewController class and has its button linked to an IBAction in the ViewController class.
I have some simple code in the IBAction that basically tells the NSTextView to change its font size to a much bigger font. I have confirmed that the code itself works when called in other methods.
The button works, BUT it is using an entirely different instance of my ViewController class. So in result: the text size doesn't change.
So my main question here is how do I get an IBAction in one window to affect an object in another window.
I hope I did an alright job at explaining myself. Keep in mind this is my first Stack Overflow question:) I tried my best to research this question but mostly found information on iOS development and using XIB files.
It sounds like you have two windows with the same controller class but want what happens in one window to affect the other window. The easiest way is going to be with notifications. When the button is clicked in one window a notification gets posted that all instances of ViewController receive and respond to by changing the font size as needed. You could also look into setting a user default when the button is clicked and using bindings to keep the text field's font size tied to the current default.

How to create custom NSStepper and NSTabView

I want re-draw these two controls, but I can not find the method to do that, anyone can give me idea to do that?
I have found some app has re-draw these controls, you can look the screenshot.
the first one is NSStepper, the second one is NSTabView,the NSTabViewItem is also be re-drawed.
You have to Subclass most of the GUI classes, if you need to customize.
For NSTabView, this is one of the class where you have to do a lot of thing yourself.
You have to draw yourself ( using beziers path )
Need to implement many methods of NSView & NSResponder for drawing and event handling.
Kindly check these links :
NSTabView with background color
http://www.positivespinmedia.com/dev/PSMTabBarControl.html
https://github.com/aaroncrespo/WILLTabView/

Xcode-style font picker

I'd like to add a font picker in my app. Xcode's Interface Builder has a great implementation which is used in Xcode's own preferences' window too.
It's the one with a little 'T' button (seemingly) inside the font name text field itself.
Is this a standard cocoa implementation?
You'll need to roll your own. See Subclassing NSControl. You'll subclass NSTextField and NSTextFieldCell. Read the entire guide, actually. Once you've got a good understanding, then you can override the drawing/geometry routines to return a rect whose width is slightly smaller (small enough to leave room for the font "button"). Then draw your font button. You'll respond to its mouse events on the font button the same as you would for any NSView.
There's no standard control for the preview text box with the button, but there is the standard NSFontPanel class that you can use to let users select the font once the 'T' button has been clicked.
Best idea is probably to override NSTextFieldCell. I've been able to get the desired appearance by overriding -drawInteriorWithFrame:inView:, and passing a slightly less wide frame when calling through to super, then drawing the button myself. You'd also have to implement the mouse tracking of the button yourself, but you could probably just create your own NSButtonCell instance and call through to it for a few methods with the sub-rect for the button.
For convenience, you may also want to create a NSTextField subclass that uses this cell instead of a straight NSTextFieldCell, but if you're loading stuff from a XIB, you can just change the class of the cell in the XIB and leave it in a regular text field view.

NSWindow with custom shadow

I want to draw a custom shadow on an NSWindow-Object.
Is there a way to do this by passing an own NSShadow-Object to NSWindow? Or a (private) method, where I can put my own drawing code?
Thanks,
Don't. You shouldn't alter the look of the window. Changing the look of UI is only allowed for Apple. Normal apps should use the standard one.
That said, there's a way, if you really insist on doing that. You can't just attach an NSShadow, unfortunately. Also, as far as I understand, there's no private method which draw the shadow. That's done by the Window Server, not by the app.
But you can ask the window server not to add the shadow. Have you noticed that in the Interface Builder, there's an option suppressing the shadow of a given window? That corresponds to the property hasShadow of an NSWindow.
After suppressing the shadow, you just need to draw everything by yourself. A nice sample code that does the custom window drawing is available at ADC, so have a look at it.

Drawing an "NSView" to a Custom-View - How? Am I taking the right approach?

I'm using Objective-C and Cocoa, whilst developing for Mac OS X - so not the iPhone/Cocoa Touch. (That said, I'd be interested if it was the same procedure for the iPhone)
I'm working on a preferences window for a simple app. I have a NSWindow with a toolbar - there are 5 different items on the toolbar, all of which need to bring up a different set of options.
So I set the NSToolbar and its items in Interface Builder, and then placed a custom view underneath the menu - taking up the rest of the window. My plan is to work out the interface too each of the NSToolbarItems options, and then draw the corresponding view on to the custom view when the specified NSToolbarItem is clicked.
I'm guessing that I simply create a NSView sub-class for each view, an empty xib in Interface Builder - set the xib to my custom NSView, code it as usual... But here's a few problems;
1 - Just how can I get the xib file to appear on the custom-view then? I have looked around and most articles don't seem to have this situation, or a situation I can relate too.
2 - When the window comes up, I want the default view to appear on the custom view. Once again, I'm guessing I just write that in the initialisation code for the NSWindow - its no big deal. It just goes back to question 1 though - how do I draw my NSView to the custom-view specified in Interface Builder?
I'd be really grateful for any help!
Cheers in advance.
So I set the NSToolbar and its items in Interface Builder, and then placed a custom view underneath the menu - taking up the rest of the window.
You can't have a menu inside of a window. You can have a pop-up button, which has a menu, but not a menu directly. Did you mean “toolbar” here?
You don't need to create a custom view for this. Make a tab view and set it to be tabless. Give it as many tab view items as you have toolbar items. In your controller, write an action method for each of the toolbar items, and in each action method, switch the active tab of the tab view.
You can activate different tabs in IB to populate them with views in IB. The active tab is saved in the nib, so make sure you set it back to the first tab before saving, so that the first tab is the one that's initially active when your app runs.
Just how can I get the xib file to appear on the custom-view then?
That question doesn't make sense.
Once again, I'm guessing I just write that in the initialisation code for the NSWindow - its no big deal.
You would only be able to do that if you have your own initialization code for the window, which you would only have if you have subclassed NSWindow. There are very few reasons to do that; unless you're making the window itself look different (not making an Aqua or HUD window), you should move that initialization code elsewhere, probably to the aforementioned controller (which should be the File's Owner of the nib).
It just goes back to question 1 though - how do I draw my NSView to the custom-view specified in Interface Builder?
A custom view in Interface Builder is a plain NSView (unless you explicitly change it to a subclass of NSView you create). However, you do not need one for anything you have described in your question.