Change NSWindow border color
1 answer
From memory, I think Tweetbot used a full borderless window and added controls for the windows themselves, but if you want AppKit to still handle those details, there is another way. If you set the window to be a textured window, you can set a custom NSColor background. This NSColor can be an image using+[NSColor colorWithPatternImage:]
I quickly knocked this down as an example, just using a solid gray as the fill, but you can paint whatever you like in this image. Then you need to set the class type of your NSWindow to your textured windows class.
SLFTexturedWindow.h
@interface SLFTexturedWindow : NSWindow
@end
SLFTexturedWindow.m
#import "SLFTexturedWindow.h"
@implementation SLFTexturedWindow
- (id)initWithContentRect:(NSRect)contentRect
styleMask:(NSUInteger)styleMask
backing:(NSBackingStoreType)bufferingType
defer:(BOOL)flag;
{
NSUInteger newStyle;
if (styleMask & NSTexturedBackgroundWindowMask) {
newStyle = styleMask;
} else {
newStyle = (NSTexturedBackgroundWindowMask | styleMask);
}
if (self = [super initWithContentRect:contentRect styleMask:newStyle backing:bufferingType defer:flag]) {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(windowDidResize:)
name:NSWindowDidResizeNotification
object:self];
[self setBackgroundColor:[self addBorderToBackground]];
return self;
}
return nil;
}
- (void)windowDidResize:(NSNotification *)aNotification
{
[self setBackgroundColor:[self addBorderToBackground]];
}
- (NSColor *)addBorderToBackground
{
NSImage *bg = [[NSImage alloc] initWithSize:[self frame].size];
// Begin drawing into our main image
[bg lockFocus];
[[NSColor lightGrayColor] set];
NSRectFill(NSMakeRect(0, 0, [bg size].width, [bg size].height));
[[NSColor blackColor] set];
NSRect bounds = NSMakeRect(0, 0, [self frame].size.width, [self frame].size.height);
NSBezierPath *border = [NSBezierPath bezierPathWithRoundedRect:bounds xRadius:3 yRadius:3];
[border stroke];
[bg unlockFocus];
return [NSColor colorWithPatternImage:bg];
}
@end
+2
source to share