【问题标题】:NSTextFieldCell standard text drawing positionNSTextFieldCell 标准文本绘制位置
【发布时间】:2023-03-09 19:55:01
【问题描述】:

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

【问题讨论】:

    标签: objective-c cocoa nstextfieldcell


    【解决方案1】:

    文本矩形是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 插图来避免在开始或停止字母超出界限时出现剪辑(放大并仔细查看比较 真正 呈现 W 或 A 字母的位置说 H 或 P)。

    【讨论】:

    • This 可能会提供一些见解。查看 drawInteriorWithFrame 方法。边框类型似乎需要插入 1 或 2 个像素。注意:在Quantum-Step中也是垂直方向;我不知道 Mac OS X 上的 Cocoa 中是否是这种情况。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多