iPhone CoreText:查找子字符串的像素坐标 [英] iPhone CoreText: Find the pixel coordinates of a substring

查看:219
本文介绍了iPhone CoreText:查找子字符串的像素坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是Twitter应用程序的屏幕截图供参考: http://screencast.com/t/YmFmYmI4M

Here's a screenshot of the twitter app for reference: http://screencast.com/t/YmFmYmI4M

我想要做的是在NSAttributedString中的子字符串顶部放置一个浮动弹出窗口,该字符串可以跨越多行。 NSAttributedString是项目的要求。

What I want to do is place a floating pop-over on top of a substring in an NSAttributedString that could span multiple lines. NSAttributedString is a requirement for the project.

在提供的屏幕截图中,您可以看到链接是背景突出显示的,因此它让我相信他们正在使用CoreText和NSAttributedStrings。我还找到了一个叫做CTRunRef的东西( http:/ /developer.apple.com/library/ios/#documentation/Carbon/Reference/CTRunRef/Reference/reference.html )看起来很有前途,但我在概念上无法将它们整合在一起。

In the screenshot supplied, you can see that links are background-highlighted, so it leads me to believe that they're using CoreText and NSAttributedStrings. I also found something called CTRunRef ( http://developer.apple.com/library/ios/#documentation/Carbon/Reference/CTRunRef/Reference/reference.html ) which looks promising, but I'm having trouble fitting it all together conceptually.

简而言之,如果我在核心文本中有一个段落,当我点击一个单词时,如何找到该单词的边界框?

In short, if I have a paragraph in core text and when I tap on a word, how do I find the bounding box for that word?

推荐答案

在属性字符串中设置一些不会影响显示的属性,但会将其作为单独的字形运行布局,然后使用CoreText布局字符串

Set some attribute in the attributed string that won't effect display, but will cause it to be laid out as a seperate glyph run, then use CoreText to layout the string

CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(attrString);
CTFrameRef ctframe = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, NULL);

现在你必须通过框架寻找相关的文本块。使用 CTFrameGetLines()获取 CTLineRef 的数组。

Now you will have to hunt through the frame to find the relevant chunk of text. Get the array of CTLineRefs with CTFrameGetLines().

遍历数组,测试触摸是否在该行上,通过检查它是否在 CTLineGetImageBounds()返回的矩形内。如果是,现在查看该行中的字形运行。

Iterate through the array, testing if the touch was on that line, by checking it is within the rect returned by CTLineGetImageBounds(). If it is, now look through the glyph runs in the line.

再次,你可以得到一个 CTRunRef s with CTLineGetGlyphRuns()。使用 CTRunGetImageBounds()检查点击是否在字形运行中,如果是,您可以找到字形运行对应的原始属性字符串中的索引范围 CTRunGetStringIndices()

Again, you can get an array of CTRunRefs with CTLineGetGlyphRuns(). Check whether the tap was within the glyph run with CTRunGetImageBounds(), and if it was you can find the the range of indices in the original attributed string that the glyph run corresponds to with CTRunGetStringIndices().

这篇关于iPhone CoreText:查找子字符串的像素坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆