Come from: http://stackoverflow.com/questions/4753282/modifying-table-headers-on-mac
首先,子类化NSTableHeaderCell
@interface CNSTableHeaderCell : NSTableHeaderCell { } - (void)drawWithFrame:(CGRect)cellFrame highlighted:(BOOL)isHighlighted inView:(NSView *)view; @end
@implementation CNSTableHeaderCell - (void)drawWithFrame:(CGRect)cellFrame highlighted:(BOOL)isHighlighted inView:(NSView *)view { CGRect cfillRect, cborderRect; NSBezierPath *path = [NSBezierPath bezierPath]; NSPoint ptStart, ptEnd; //make two new rect from cellFrame //for instance,based on the fourth argument of CGRectDivide -- in this case is 1.0 //cellFrame = (0,0,200,20),cfillRect = (0,0,200,20-1),cborderRect = (0,20-1,200,1) CGRectDivide(cellFrame, &cborderRect, &cfillRect, 1.0, CGRectMaxYEdge); //make NSRect from CGRect, and fill it with any color you like,in this case is blueColor NSRect fillRect = NSMakeRect(cfillRect.origin.x, cfillRect.origin.y, cfillRect.size.width, cfillRect.size.height); [[NSColor blueColor] set]; NSRectFill(fillRect); //set highlight behavior if (isHighlighted) { [[NSColor colorWithDeviceWhite:0.0 alpha:0.1] set]; NSRectFillUsingOperation(fillRect, NSCompositeSourceOver); } //set antialias to make text more smoothly [[NSGraphicsContext currentContext] setShouldAntialias:YES]; //draw right vertical line for cell ptStart.x = fillRect.origin.x + fillRect.size.width - 1; ptStart.y = fillRect.origin.y; ptEnd.x = fillRect.origin.x + fillRect.size.width - 1; ptEnd.y = fillRect.origin.y + fillRect.size.height; [path moveToPoint:ptStart]; [path lineToPoint:ptEnd]; //draw cell bottom line ptStart.x = cellFrame.origin.x; ptStart.y = cellFrame.origin.y + cellFrame.size.height; ptEnd.x = cellFrame.origin.x + cellFrame.size.width; ptEnd.y = cellFrame.origin.y + cellFrame.size.height; [path moveToPoint:ptStart]; [path lineToPoint:ptEnd]; //draw cell top line ptStart.x = fillRect.origin.x; ptStart.y = fillRect.origin.y; ptEnd.x = fillRect.origin.x + fillRect.size.width; ptEnd.y = fillRect.origin.y; [path moveToPoint:ptStart]; [path lineToPoint:ptEnd]; //set line color and show line [[NSColor redColor] set]; [path stroke]; //finally draw interior [self drawInteriorWithFrame:fillRect inView:view]; } //NSCell method - (void)drawWithFrame:(CGRect)cellFrame inView:(NSView *)view { [self drawWithFrame:cellFrame highlighted:NO inView:view]; } //NSCell method - (void)highlight:(BOOL)isHighlighted withFrame:(CGRect)cellFrame inView:(NSView *)view { [self drawWithFrame:cellFrame highlighted:isHighlighted inView:view]; } @end
然后,应用到tableview
NSTableColumn *aColumn = nil; NSArray *arrTableColumns = [self tableColumns]; for (int i = 0;i<[arrTableColumns count];i++) { aColumn = [arrTableColumns objectAtIndex:i]; if ([[aColumn headerCell]class] == [NSTableHeaderCell class]) { CNSTableHeaderCell *aHeaderCell = [[CNSTableHeaderCell alloc] initTextCell:[[aColumn headerCell] stringValue]]; [aHeaderCell setFont:[NSFont fontWithName:@"Arial" size:13]]; [aHeaderCell setTextColor:[NSColor whiteColor]]; [aColumn setHeaderCell:aHeaderCell]; [aHeaderCell release]; } }
效果如图: