2013-02-26 2 views
0

JUEmptyView (모든 하위보기가 제거되었을 때보기 중간에 맞춤 가운데 정렬 된 자리 표시자를 표시하는 사용자 정의 코코아 컨트롤/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

확인, 그래서 내가 그것을 해결하기 위해 관리했습니다 방법입니다.

부모보기 중간에 레이블이있는 둥근 상자를 인쇄하면 기본 배경을 다시 그리지 못했습니다. "빈보기"구현이 나타납니다. 그래서, 걸리는 모든 drawRect:에서

단지 추가 ...를 다시 칠하는 것입니다

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