Cocoa (Mac) 中的命中测试文本?
有没有办法对文本进行命中测试?例如,如果我在屏幕上的边界框内绘制文本“Hello graph!”,我如何知道这些空白矩形(空格)的尺寸和位置:
- 字母“e”上方的空白
- 矩形从字母“o”上方到字母“p”上方的空白矩形
- 字母“H”下方的空白矩形到“g”之前的空格
答案。
等,换句话说,您知道如何找出 哪些区域被实际文本占据以及哪些区域是真正的空白。
平台(图形库)是 Mac OS X 上的 Cocoa,字符集是 Unicode(这可能排除原始 CoreGraphics API?),并且字体可以是 Cocoa 可以使用的任何内容
。
Is there a way for hit-testing text? That is for example if I draw text "Hello graph!"" on the screen, within a bounding box, how can I tell the dimension and position of these blank rectangles (blank spaces):
- The blank rectangle above the letter "e"
- The blank rectangle that spans from above the letter "o" up to above the letter "p"
- The blank rectangle below the letter "H" up to the space before "g"
etc, you get the idea.
In other words how to find out which areas occupied by actual text and which areas that are really blank.
The platform (graphics library) is Cocoa on Mac OS X, the character set is Unicode (which probably rules out raw CoreGraphics API?), and the font can be anything that Cocoa can use.
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以像这样在
NSTextView
中获取字符的矩形:然后,获取您想要的
NSGlyph
:并在
NSBezierPath
中绘制它:接下来,查询路径的边界:
然后您可以比较这两个矩形。
看一下 NSLayoutManager 类参考,文本系统概述 和 文本布局编程指南< /a>.
You can get the rect of a character in a
NSTextView
like this:Then, get the
NSGlyph
that you want:And draw it in an
NSBezierPath
:Next, query the path for its bounds:
You can then compare those two rectangles.
Have a look at the NSLayoutManager Class Reference, the Text System Overview, and the Text Layout Programming Guide.
您可以在完全透明的位图上绘制文本(通过 CGBitmapContext ),然后检查该位图的特定点是否透明。
如果您需要一个矩形列表(在 2d 图形中称为 region),您必须自己实现或通过 google 查找库,因为 CoreGraphics 和 Cocoa 没有区域(至少记录)。
You could draw the text over fully-transparent bitmap (via
CGBitmapContext
), then check that bitmap if a particular point is transparent or not.If you need a list of rectangles (that is called region in 2d graphics), you'll have to implement that yourself or google for a library, as CoreGraphics and Cocoa don't have regions (at least documented).