gpt4 book ai didi

cocoa - Cocoa (Mac) 中的 HitTest 文本?

转载 作者:行者123 更新时间:2023-12-03 16:38:38 24 4
gpt4 key购买 nike

有没有办法对文本进行 HitTest ?例如,如果我在屏幕上的边界框内绘制文本“Hello graph!”,我如何知道这些空白矩形(空白)的尺寸和位置:

  • 字母“e”上方的空白矩形
  • 从字母“o”上方到字母“p”上方的空白矩形
  • 字母“H”下方的空白矩形到“g”之前的空格

等等,你明白了。

换句话说,如何找出哪些区域被实际文本占据,哪些区域实际上是空白的。

平台(图形库)是 Mac OS X 上的 Cocoa,字符集是 Unicode(这可能排除原始 CoreGraphics API?),并且字体可以是 Cocoa 可以使用的任何内容。

提前致谢

最佳答案

您可以在 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 Class ReferenceText System Overview ,以及 Text Layout Programming Guide .

关于cocoa - Cocoa (Mac) 中的 HitTest 文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7050307/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com