Cocoa (Mac) 中的命中测试文本?

发布于 2024-11-29 04:33:27 字数 343 浏览 2 评论 0原文

有没有办法对文本进行命中测试?例如,如果我在屏幕上的边界框内绘制文本“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 技术交流群。

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

发布评论

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

评论(2

帅的被狗咬 2024-12-06 04:33:27

您可以像这样在 NSTextView 中获取字符的矩形:

//Get the range of characters, which may be different than the range of glyphs
NSRange range = [[textView layoutManager]glyphRangeForCharacterRange:NSMakeRange(charIndex, 0) actualCharacterRange:NULL]

//Get the rect of the character
NSRect rect = [[textView layoutManager]boundingRectForGlyphRange:range inTextContainer:[textView textContainer]];

然后,获取您想要的 NSGlyph

NSGlyph glyph = [[textView layoutManager]glyphAtIndex:range.location];

并在 NSBezierPath 中绘制它:

NSBezierPath *path = [NSBezierPath bezierPath];
[path appendBezierPathWithGlyph:glyph inFont:myFavoriteFont];

接下来,查询路径的边界:

NSRect actualRect = [path bounds];

然后您可以比较这两个矩形。

看一下 NSLayoutManager 类参考文本系统概述 和 文本布局编程指南< /a>.

You can get the rect of a character in a NSTextView like this:

//Get the range of characters, which may be different than the range of glyphs
NSRange range = [[textView layoutManager]glyphRangeForCharacterRange:NSMakeRange(charIndex, 0) actualCharacterRange:NULL]

//Get the rect of the character
NSRect rect = [[textView layoutManager]boundingRectForGlyphRange:range inTextContainer:[textView textContainer]];

Then, get the NSGlyph that you want:

NSGlyph glyph = [[textView layoutManager]glyphAtIndex:range.location];

And draw it in an NSBezierPath:

NSBezierPath *path = [NSBezierPath bezierPath];
[path appendBezierPathWithGlyph:glyph inFont:myFavoriteFont];

Next, query the path for its bounds:

NSRect actualRect = [path 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.

欢你一世 2024-12-06 04:33:27

您可以在完全透明的位图上绘制文本(通过 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).

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