可可控制文本位置?
是否可以获取在标准(非自定义)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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不幸的是,没有一个解决方案适用于所有控件和单元格。 您应该能够使用这些方法获得此信息的良好近似值:
以下是一些适用于 NSButton/NSButtonCell 的代码
此时,基线位于按钮的(边界)坐标空间中。 您可能想使用 -[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:
Here's some code that works for NSButton/NSButtonCell
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.