我正在努力寻找一种优雅的方式,以垂直方式显示表格视图的列标题(从传统方向逆时针旋转90度)。作为一个真正的NSTableHeaderCell,我没有结婚,我认为通过覆盖NSTextFieldCell或NSCell可能更容易做到这一点。
单元格只包含不可编辑的文本,但通常是两行,并且有时会根据上下文对列的其余部分进行着色。
我似乎无法找到任何这样做的可可应用程序,更不用说开源示例了。有什么想法吗?
答案 0 :(得分: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