2013-02-26 67 views
0

我一直在試驗JUEmptyView(一個定製的Cocoa控件/ NSView子類,當所有子視圖都被刪除時,它在視圖的中間顯示一個自定義的中心對齊的佔位符)。爲NSTabView實現「空視圖」

所以,我試圖實現一個NSTabView相同的東西 - 簡單地通過使它成爲NSTabView子類(並重新設置我的初始NSTabView項目類)。

一般來說,它確實工作 - 它確實顯示佔位符(當最後一個標籤關閉時)。 然而,一些問題依然存在:

  • 背景仍是一個TabItem的的(認爲他們已經全部被關閉)
  • 當調整窗口大小(假定NSTabView一直延伸從左方式正確和從上到下),視圖看起來好像不能正確重繪自己。

實施例:

enter image description here

全碼:

接口

#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 

回答

0

OK,所以這就是我是如何設法解決它。

原來這個「空視圖」的實現,除了在父視圖的正中間打印一個帶有標籤的圓形框以外,未能重新繪製主背景。因此,所需要的是重新繪製它...

drawRect:只需添加:

[[NSColor grayColor] set]; // or any other color you prefer 
NSRectFill([self bounds]);