我的应用程序中有一个NSTableView,在X轴和Y轴上都绘制了数据(即每行都与每列匹配)。我已经以我喜欢的方式获得了填充单元格的数据,但是水平伸展的列看起来很糟糕。
我想把NSTextFieldCell翻过来,这样文本就是垂直书写的,而不是水平书写的。我意识到我可能不得不将NSTextFieldCell子类化,但我不确定我需要重写哪些函数才能完成我想要做的事情。
NSTextFieldCell中的哪些函数可以绘制文本本身?有没有什么内置的方法可以垂直而不是水平地绘制文本?
发布于 2010-07-01 05:48:38
嗯,我花了很多时间才弄明白这个问题,但我最终遇到了NSAffineTransform对象,它显然可以用来相对于应用程序移动整个坐标系。弄清楚这一点后,我继承了NSTextViewCell并重写了-drawInteriorWithFrame:inView:函数,以便在绘制文本之前旋转坐标系。
- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
// Save the current graphics state so we can return to it later
NSGraphicsContext *context = [NSGraphicsContext currentContext];
[context saveGraphicsState];
// Create an object that will allow us to shift the origin to the center
NSSize originShift = NSMakeSize(cellFrame.origin.x + cellFrame.size.width / 2.0,
cellFrame.origin.y + cellFrame.size.height / 2.0);
// Rotate the coordinate system
NSAffineTransform* transform = [NSAffineTransform transform];
[transform translateXBy: originShift.width yBy: originShift.height]; // Move origin to center of cell
[transform rotateByDegrees:270]; // Rotate 90 deg CCW
[transform translateXBy: -originShift.width yBy: -originShift.height]; // Move origin back
[transform concat]; // Set the changes to the current NSGraphicsContext
// Create a new frame that matches the cell's position & size in the new coordinate system
NSRect newFrame = NSMakeRect(cellFrame.origin.x-(cellFrame.size.height-cellFrame.size.width)/2,
cellFrame.origin.y+(cellFrame.size.height-cellFrame.size.width)/2,
cellFrame.size.height, cellFrame.size.width);
// Draw the text just like we normally would, but in the new coordinate system
[super drawInteriorWithFrame:newFrame inView:controlView];
// Restore the original coordinate system so that other cells can draw properly
[context restoreGraphicsState];
}我现在有了一个可以横向绘制其内容的NSTextCell!通过改变行高,我可以给它足够的空间让它看起来更好。
https://stackoverflow.com/questions/3136302
复制相似问题