我一直在尝试使用JUEmptyView(一个自定义的 Cocoa 控件/NSView
子类,当所有子视图都被删除时,它会在视图中间显示一个自定义的中心对齐占位符)。
因此,我尝试为 an 实现相同的事情NSTabView
- 只需将其NSTabView
设为子类(并重新设置我的初始NSTabView
项目类)。
通常,它确实有效 - 它确实显示了占位符(当最后一个选项卡关闭时)。但是,仍然存在一些问题:
- 背景仍然是一个 tabitem 的背景(以为它们都已经关闭了)
- 调整窗口大小时(假设
NSTabView
从左到右和从上到下一直延伸),视图似乎无法正确重绘自身。
例子 :
完整代码:
界面
#import <Cocoa/Cocoa.h>
@interface JUTabEmptyView : NSTabView
{
@private
BOOL forceShow;
NSString *title;
NSColor *titleColor;
NSFont *titleFont;
NSColor *backgroundColor;
}
@property (nonatomic, copy) NSString *title;
@property (nonatomic, retain) NSFont *titleFont;
@property (nonatomic, retain) NSColor *titleColor;
@property (nonatomic, retain) NSColor *backgroundColor;
@property (nonatomic, assign) BOOL forceShow;
- (id)initWithTitle:(NSString *)title;
- (id)initWithTitle:(NSString *)title andFont:(NSFont *)font;
- (id)initWithTitle:(NSString *)title font:(NSFont *)font color:(NSColor *)color andBackgroundColor:(NSColor *)backgroundColor;
@end
执行
#import "JUTabEmptyView.h"
@implementation JUTabEmptyView
@synthesize title, titleFont, titleColor, backgroundColor;
@synthesize forceShow;
#pragma mark -
#pragma mark Setter
- (void)setTitle:(NSString *)ttitle
{
[title autorelease];
title = [ttitle copy];
[self setNeedsDisplay:YES];
}
- (void)setTitleFont:(NSFont *)ttitleFont
{
[titleFont autorelease];
titleFont = [ttitleFont retain];
[self setNeedsDisplay:YES];
}
- (void)setTitleColor:(NSColor *)ttitleColor
{
[titleColor autorelease];
titleColor = [ttitleColor retain];
[self setNeedsDisplay:YES];
}
- (void)setBackgroundColor:(NSColor *)tbackgroundColor
{
[backgroundColor autorelease];
backgroundColor = [tbackgroundColor retain];
[self setNeedsDisplay:YES];
}
- (void)setForceShow:(BOOL)tforceShow
{
forceShow = tforceShow;
[self setNeedsDisplay:YES];
}
#pragma mark -
#pragma Drawing
- (void)drawRect:(NSRect)dirtyRect
{
if(forceShow || [[self subviews] count] == 0)
{
NSRect rect = [self bounds];
NSSize size = [title sizeWithAttributes:[NSDictionary dictionaryWithObject:titleFont forKey:NSFontAttributeName]];
NSSize bezierSize = NSMakeSize(size.width + 40.0, size.height + 20.0);
NSRect drawRect;
// Background
drawRect = NSMakeRect(0.0, 0.0, bezierSize.width, bezierSize.height);
drawRect.origin.x = round((rect.size.width * 0.5) - (bezierSize.width * 0.5));
drawRect.origin.y = round((rect.size.height * 0.5) - (bezierSize.height * 0.5));
[backgroundColor setFill];
[[NSBezierPath bezierPathWithRoundedRect:drawRect xRadius:8.0 yRadius:8.0] fill];
// String
drawRect = NSMakeRect(0.0, 0.0, size.width, size.height);
drawRect.origin.x = round((rect.size.width * 0.5) - (size.width * 0.5));
drawRect.origin.y = round((rect.size.height * 0.5) - (size.height * 0.5));
[title drawInRect:drawRect withAttributes:[NSDictionary dictionaryWithObjectsAndKeys:titleColor, NSForegroundColorAttributeName,
titleFont, NSFontAttributeName, nil]];
}
}
- (void)willRemoveSubview:(NSView *)subview
{
[super willRemoveSubview:subview];
[self setNeedsDisplay:YES];
}
- (void)didAddSubview:(NSView *)subview
{
[super didAddSubview:subview];
[self setNeedsDisplay:YES];
}
#pragma mark -
#pragma mark Constructor / Destructor
- (void)constructWithTitle:(NSString *)ttitle font:(NSFont *)font color:(NSColor *)color andBackgroundColor:(NSColor *)tbackgroundColor
{
title = ttitle ? [ttitle copy] : [[NSString alloc] initWithString:@"No active document"];
titleFont = font ? [font retain] : [[NSFont boldSystemFontOfSize:[NSFont smallSystemFontSize]] retain];
titleColor = color ? [color retain] : [[NSColor colorWithCalibratedRed:0.890 green:0.890 blue:0.890 alpha:1.0] retain];
backgroundColor = tbackgroundColor ? [tbackgroundColor retain] : [[NSColor colorWithCalibratedRed:0.588 green:0.588 blue:0.588 alpha:1.000] retain];
}
- (id)initWithCoder:(NSCoder *)decoder
{
if(self = [super initWithCoder:decoder])
{
[self constructWithTitle:nil font:nil color:nil andBackgroundColor:nil];
}
return self;
}
- (id)initWithFrame:(NSRect)frameRect
{
if(self = [super initWithFrame:frameRect])
{
[self constructWithTitle:nil font:nil color:nil andBackgroundColor:nil];
}
return self;
}
- (id)initWithTitle:(NSString *)ttitle
{
if((self = [super init]))
{
[self constructWithTitle:ttitle font:nil color:nil andBackgroundColor:nil];
}
return self;
}
- (id)initWithTitle:(NSString *)ttitle andFont:(NSFont *)font
{
if((self = [super init]))
{
[self constructWithTitle:ttitle font:font color:nil andBackgroundColor:nil];
}
return self;
}
- (id)initWithTitle:(NSString *)ttitle font:(NSFont *)font color:(NSColor *)color andBackgroundColor:(NSColor *)tbackgroundColor
{
if((self = [super init]))
{
[self constructWithTitle:ttitle font:font color:color andBackgroundColor:tbackgroundColor];
}
return self;
}
- (void)dealloc
{
[title release];
[titleFont release];
[titleColor release];
[backgroundColor release];
[super dealloc];
}
@end