Yesterday Brent Simmons posted his first go at using IB_DESIGNABLE and IBInspectable, for a simple
NSView subclass to provide a
backgroundColor property to a view.
I've had a similar simple class for a while, so that inspired me to push it to GitHub as open source, too. My variation provides four new properties, to set either or both the background and border colors:
drawsBackground BOOLproperty. Defaults to YES.
drawsBorder BOOLproperty. Defaults to YES.
backgroundColor NSColorproperty. Defaults to nil, but lazily assigns a light blue color if not set to another color.
borderColor NSColorproperty. Defaults to nil, but lazily assigns a light gray color if not set to another color.
To use it, simply include the
DejalBackgroundView.m files in your project, then change a container
DejalBackgroundView in the view hierarchy, and set the colors as desired (via code or IB), e.g.:
self.backgroundView.backgroundColor = [NSColor lightGrayColor];
If you only want a background without a border, easily turn off the border drawing:
self.drawsBorder = NO;
All of these properties can be set in IB thanks to the IB_DESIGNABLE and IBInspectable attributes in Xcode 6.
As a bonus, the view is automatically treated as opaque if the background color is used, and is itself opaque.
This is a very simple class, but quite useful.
You can get the code and more information from the Dejal Open Source page.
(I've been meaning to push out more of my code as open source; I would like to take the time to release more, to give back to the community. Please let me know if you read this and would like to encourage me to do so.)