2010-04-29 77 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