首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >一种实现横向NSTextFieldCell的方法?

一种实现横向NSTextFieldCell的方法?
EN

Stack Overflow用户
提问于 2010-06-29 05:33:24
回答 1查看 376关注 0票数 0

我的应用程序中有一个NSTableView,在X轴和Y轴上都绘制了数据(即每行都与每列匹配)。我已经以我喜欢的方式获得了填充单元格的数据,但是水平伸展的列看起来很糟糕。

我想把NSTextFieldCell翻过来,这样文本就是垂直书写的,而不是水平书写的。我意识到我可能不得不将NSTextFieldCell子类化,但我不确定我需要重写哪些函数才能完成我想要做的事情。

NSTextFieldCell中的哪些函数可以绘制文本本身?有没有什么内置的方法可以垂直而不是水平地绘制文本?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-07-01 05:48:38

嗯,我花了很多时间才弄明白这个问题,但我最终遇到了NSAffineTransform对象,它显然可以用来相对于应用程序移动整个坐标系。弄清楚这一点后,我继承了NSTextViewCell并重写了-drawInteriorWithFrame:inView:函数,以便在绘制文本之前旋转坐标系。

代码语言:javascript
复制
- (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!通过改变行高,我可以给它足够的空间让它看起来更好。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3136302

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档