2010-04-29 25 views
2

我努力尋找一種垂直方式(從傳統逆時針旋轉90度)顯示錶視圖列標題的優雅方式。我並沒有把這個做成一個真正的NSTableHeaderCell,我想通過重寫NSTextFieldCell或NSCell可能會更容易。NSTextFieldCell或只與NSCell垂直文本(和彩色着色)

單元格只包含不可編輯的文本,但它通常是兩行,根據上下文它有時會與列的其餘部分一起着色。

我似乎無法找到任何這樣做的可可應用程序,更不用說開源示例。有什麼想法嗎?

回答

2
#import "VerticalTextCell.h" 

@implementation VerticalTextCell 

- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView { 

    NSMutableDictionary *atr = [NSMutableDictionary dictionary]; 
    NSFont *font = [NSFont fontWithName:@"Lucida Grande" size:12]; 
    [atr setObject:font forKey:NSFontAttributeName]; 

    [[[self backgroundColor] colorWithAlphaComponent:0.7] set]; 
    NSRectFillUsingOperation(cellFrame, NSCompositeSourceOver); 

    NSGraphicsContext *currentContext = [NSGraphicsContext currentContext]; 
    [currentContext saveGraphicsState]; 

    NSAffineTransform *transform = [NSAffineTransform transform]; 
    [transform translateXBy:NSMinX(cellFrame) yBy:NSMinY(cellFrame)]; 
    [transform rotateByDegrees:-90]; 
    [transform concat]; 

    // vertical inset 5 pixels 
    [[self stringValue] drawInRect:NSMakeRect(-NSHeight(cellFrame),5,NSHeight(cellFrame),NSWidth(cellFrame)) withAttributes:atr]; 

    [currentContext restoreGraphicsState]; 

} 


@end