【问题标题】:NSTextFieldCell or just NSCell with vertical text (and colored tinting)NSTextFieldCell 或仅带有垂直文本(和彩色着色)的 NSCell
【发布时间】:2010-04-29 05:57:09
【问题描述】:
我正在努力寻找一种优雅的方式来以垂直方式显示表格视图的列标题(从传统方式逆时针旋转 90 度)。我不喜欢作为一个实际的 NSTableHeaderCell 来做这件事,我认为通过覆盖 NSTextFieldCell 或 NSCell 可能会更容易。
单元格仅包含不可编辑的文本,但通常是两行,有时会根据上下文与列的其余部分着色。
我似乎找不到任何这样做的可可应用程序,更不用说开源示例了。有什么想法吗?
【问题讨论】:
标签:
objective-c
cocoa
nstextfieldcell
nscell
【解决方案1】:
#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