当你在drawInteriorWithFrame:inView:中调用[super drawInteriorWithFrame:cellFrame inView:controlView]时,有没有办法获取NSTextFieldCell中文本的绘制位置?
最佳答案
文本矩形是通过 cellFrame
传递的 titleRectForBounds:
的返回值,带有神秘的 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 插入以避免在开始或停止字母超出范围时剪切(放大并仔细观察 W 或 A 字母真正渲染的位置,比较说 H 或 P)。
关于objective-c - NSTextFieldCell 标准文本绘制位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17896590/