1

当您在 drawInteriorWithFrame:inView: 中调用 [super drawInteriorWithFrame:cellFrame inView:controlView] 时,有没有办法获取 NSTextFieldCell 中文本的绘制位置?

4

1 回答 1

2

文本矩形是titleRectForBounds:通过cellFrame神秘的 2px 水平插入的返回值:

// this code ...
NSRect bounds = NSInsetRect(cellFrame, 2, 0); // dont ask me why
NSRect titleRect = [self titleRectForBounds:bounds];
[self.attributedStringValue drawInRect:titleRect];

// ... is equivalent to
[super drawInteriorWithFrame:cellFrame inView:controlView];

我不确定那些 2px 代表什么,它是通过尝试使用所有可能的文本字段配置从 10.6 到 10.9-preview 的所有操作系统通过实验得出的。似乎总是有这个无条件的 2px 插图。NSInsetRect通过设置断点并查看调试器反汇编,可以轻松地确认默认实现确实使用这些参数调用。

我的猜测:为了更好的排版,需要 2px 插图来避免在开始或停止字母超出范围时剪裁(放大并仔细查看与H 或 P 相比,W 或 A 字母真正呈现的位置)。

于 2013-07-27T10:37:18.947 回答