web-dev-qa-db-ja.com

OSXでNSWindowタイトルバーの色を変更する方法

NSWindowタイトルバーの色とタイトルの色に関する長い検索の結果、簡単な描画ソリューションを見つけました。私の知識を共有するためにこれを投稿します。

20
Muruganandham K

NSViewという名前のMyTitleViewをサブクラス化し、次のコードを追加します

- (void)drawString:(NSString *)string inRect:(NSRect)rect {
    static NSDictionary *att = nil;
    if (!att) {
        NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
        [style setLineBreakMode:NSLineBreakByTruncatingTail];
        [style setAlignment:NSCenterTextAlignment];
        att = [[NSDictionary alloc] initWithObjectsAndKeys: style, NSParagraphStyleAttributeName,[NSColor whiteColor], NSForegroundColorAttributeName,[NSFont fontWithName:@"Helvetica" size:12], NSFontAttributeName, nil];
        [style release];

    }

    NSRect titlebarRect = NSMakeRect(rect.Origin.x+20, rect.Origin.y-4, rect.size.width, rect.size.height);


    [string drawInRect:titlebarRect withAttributes:att];
}


- (void)drawRect:(NSRect)dirtyRect
{
    NSRect windowFrame = [NSWindow  frameRectForContentRect:[[[self window] contentView] bounds] styleMask:[[self window] styleMask]];
    NSRect contentBounds = [[[self window] contentView] bounds];

    NSRect titlebarRect = NSMakeRect(0, 0, self.bounds.size.width, windowFrame.size.height - contentBounds.size.height);
    titlebarRect.Origin.y = self.bounds.size.height - titlebarRect.size.height;

    NSRect topHalf, bottomHalf;
    NSDivideRect(titlebarRect, &topHalf, &bottomHalf, floor(titlebarRect.size.height / 2.0), NSMaxYEdge);

    NSBezierPath * path = [NSBezierPath bezierPathWithRoundedRect:self.bounds xRadius:4.0 yRadius:4.0];
    [[NSBezierPath bezierPathWithRect:titlebarRect] addClip];



    NSGradient * gradient1 = [[[NSGradient alloc] initWithStartingColor:[NSColor colorWithCalibratedWhite:0.0 alpha:1.0] endingColor:[NSColor colorWithCalibratedWhite:1 alpha:1.0]] autorelease];
    NSGradient * gradient2 = [[[NSGradient alloc] initWithStartingColor:[NSColor colorWithCalibratedWhite:1 alpha:1.0] endingColor:[NSColor colorWithCalibratedWhite:0 alpha:1.0]] autorelease];

  [path addClip];


//    [[NSColor colorWithCalibratedWhite:0.00 alpha:1.0] set];
//   [path fill];


   [gradient1 drawInRect:topHalf angle:270.0];
    [gradient2 drawInRect:bottomHalf angle:270.0];

    [[NSColor blackColor] set];
    NSRectFill(NSMakeRect(0, -4, self.bounds.size.width, 1.0));


    [self drawString:@"My Title" inRect:titlebarRect];


}

AppDelegateでクラスMyTitleViewをインポートし、次のコードを追加します

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    NSRect boundsRect = [[[_window contentView] superview] bounds];
    BlackTitlebarView * titleview = [[BlackTitlebarView alloc] initWithFrame:boundsRect];
    [titleview setAutoresizingMask:(NSViewWidthSizable | NSViewHeightSizable)];

    [[[_window contentView] superview] addSubview:titleview positioned:NSWindowBelow relativeTo:[[[[_window contentView] superview] subviews] objectAtIndex:0]];
}
24
Muruganandham K

これを行うには、はるかに簡単な方法があります。 Storyboard/.xibファイルでウィンドウの外観を「テクスチャ」に変更できることを読みましたが、完全には機能しません(タイトルバーの色がまだらになっています)。ただし、コードの色は2行で変更できます。

window.titlebarAppearsTransparent = true // gives it "flat" look
window.backgroundColor = <NSColor> // set the background color
23
Sam Claus

OSX10.10では警告メッセージはありません。そのように

NSView * windowTopView = [[_window standardWindowButton:NSWindowCloseButton] superview];

-(void) applicationDidFinishLaunching:(NSNotification *)aNotification {

NSRect boundsRect = [windowTopView bounds];

NSView *windowTopView = [[_window standardWindowButton:NSWindowCloseButton] superview];
BTitleView * titleview = [[BTitleView alloc] initWithFrame:boundsRect];
[titleview setTitleImagePath:[[NSBundle mainBundle] pathForResource:@"Top1-01" ofType:@"bmp"]] ;
[titleview setAutoresizingMask:(NSViewWidthSizable | NSViewHeightSizable)];
[windowTopView addSubview:titleview positioned:NSWindowBelow relativeTo:[[windowTopView subviews] objectAtIndex:0]];

//addTitlebarAccessoryViewController
NSTitlebarAccessoryViewController *dummyTitlebarAccessoryViewController;
NSView * logoview = [[NSView alloc] initWithFrame:NSMakeRect(0, 0, 10, 11)];
dummyTitlebarAccessoryViewController = [NSTitlebarAccessoryViewController new];
dummyTitlebarAccessoryViewController.view = logoview;
dummyTitlebarAccessoryViewController.fullScreenMinHeight = 0;
[_window addTitlebarAccessoryViewController:dummyTitlebarAccessoryViewController];


}
1
ByungBok Lee