views:

1021

answers:

3

I want to set the background of an NSBox to be a gradient. In Interface Builder it is possible to set the background color of an NSBox to selectedMenuColor which is a gradient.

NSBox only has a setFillColor method so how is Interface Builder filling it with a gradient?

How do I programmatically fill an NSBox without subclassing it? It would be trivial to subclass NSBox but the workings of Interface Builder suggest there may be better solution.

A: 

Probably the closest you could come would be to use an NSColor created with colorWithPatternImage:, then create the gradient you want as an image and load that in. Ugly, but should work. I think subclassing is your best bet.

Ben Gottlieb
A: 

The selectedMenuColor color is actually a pre-rendered image of a gradient, and not a gradient drawn on the fly, so there is not any way to specify an arbitrary gradient as a background color. Like Ben said, subclassing is probably the way to go.

Brian Webster
+1  A: 

selectedMenuColor is a "magic" color that is not displayed as a solid color. Many of these "magic" colors exist in the system.

I have used colorWithPatternImage: for this before. But note that the image you use as the pattern will get tiled, so you will probably have to resize the image to the size of the box.

Dave Dribin