NSTextFieldCell or just NSCell with vertical text (and color shading)

I am struggling to find an elegant way to display column headers for a table view vertically (rotated 90 degrees counterclockwise from traditional). I'm not married to do this as a real NSTableHeaderCell, I figured it would be easier to do this by overriding NSTextFieldCell or NSCell.

The cells contain only unedited text, but they are usually two lines, and sometimes they are colored with the rest of the column depending on the context.

I can't find any cocoa apps that do this, much less an open source example. Any thoughts?

+2


a source to share


1 answer


#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

      



+2


a source







All Articles