可可控制文本位置?

发布于 2024-07-29 02:24:35 字数 121 浏览 12 评论 0原文

是否可以获取在标准(非自定义)Cocoa 控件中绘制文本的坐标? 实际上,我需要文本的基线,即 y 轴偏移值(相对于视图框架矩形的 y 原点)。 这是选择“布局”->“显示布局矩形”时界面生成器在设计窗格上显示的内容。

Is this possible to get the coordinates of where the text drawn in standard (not custom) Cocoa controls? Actually, I need a baseline of the text, the y-axis offset value (relative to the y-origin of the view’s frame rectangle).
This is what the Interface Builder shows on design pane when Layout->Show Layout Rectangles selected.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花心好男孩 2024-08-05 02:24:35

不幸的是,没有一个解决方案适用于所有控件和单元格。 您应该能够使用这些方法获得此信息的良好近似值:

-[NSCell titleRectForBounds:]
-[NSCell font]
-[NSFont ascender]

以下是一些适用于 NSButton/NSButtonCell 的代码

NSRect titleRect = [[button cell] titleRectForBounds:[button bounds]];
CGFloat baseline = ceil(NSMinY(titleRect) + [[[button cell] font] ascender]);

此时,基线位于按钮的(边界)坐标空间中。 您可能想使用 -[NSView ConvertPoint:toView:]; 将其转换为其他空间

另外,那里的“天花板”是一个近似值。 并非所有控件都会这样做。 有些可能会取整或使用其他舍入函数。 或者他们可能会完全不同地布局标题,并且这种近似将不起作用。

Unfortunately, there isn't a single solution that works for all controls and cells. You should be able to get a good approximation of this information with these methods:

-[NSCell titleRectForBounds:]
-[NSCell font]
-[NSFont ascender]

Here's some code that works for NSButton/NSButtonCell

NSRect titleRect = [[button cell] titleRectForBounds:[button bounds]];
CGFloat baseline = ceil(NSMinY(titleRect) + [[[button cell] font] ascender]);

At this point, baseline is in the button's (bounds) coordinate space. You might want to convert it to some other space with -[NSView convertPoint:toView:];

Also, that "ceil" in there is an approximation. Not all controls will do that. Some might floor, or use some other rounding function. Or they might layout their title's completely differently, and this approximation won't work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文