CoreText 映射字符
我在触摸处理程序中有一些,它响应我在其中绘制了一些属性文本的视图上的点击。通过这个,我已经达到了我有一个 CTRunRef
(和相关行)以及该运行中的字形数量。
我无法轻松弄清楚的是,如何获取该运行的字形,并根据我的属性字符串,将其映射到字符串中的字符。
具体来说,问题是我想知道用户在视图中点击了哪个单词,这样我就可以处理该单词是否是 URL 并触发自定义委托方法,以便我可以用它打开一个 Web 视图。我拥有所有可能的子字符串,我只是不知道如何将用户点击的位置映射到特定的子字符串。
任何帮助将不胜感激。
更新:实际上,根据 stackoverflow 上另一个人的建议,我已经采用了不同的方式。基本上,我所做的就是设置一个自定义属性 @"MyAppLinkAddress"
以及我在将字符串转换为属性字符串时找到的 URL 的值。这发生在我绘制字符串之前。因此,当发生点击事件时,我只是检查该属性是否存在,如果存在,则调用我的委托方法,如果不存在,则忽略它。它现在正在按照我想要的方式工作,但我将把这个问题再留几天,如果有人能想出答案,如果它是一个可行的解决方案,我会很乐意接受它,以便其他人也许在将来的某个时候会发现这些信息很有用。
I have some in a touch handler which responds to a tap on a view that I've drawn some attributed text in. through this, I've got to the point where I have a CTRunRef
(and the associated line) as well as the number of glyphs in that run.
What I'm not able to figure out easily, is how I can take that run of glyphs and, given my attributed string, map it out to characters in the string.
Specifically the problem is I would like to know what word the user tapped on in the view, so I can process whether or not that word is a URL and fire off a custom delegate method so I can open a web view with it. I have all the possible substrings, I just don't know how to map where the user tapped to a particular substring.
Any help would be greatly appreciated.
UPDATE: I've actually gone and done it a different way, on the suggestion of another person off of stackoverflow. Basically what I've done is to set a custom attribute, @"MyAppLinkAddress"
with the value of the URL I found when I was converting the string to an attributed string. This happens before I draw the string. Therefore, when a tap event occurs, I just check if that attribute exists, and if so, call my delegate method, if not, just ignore it. It is working how I'd like now, but I'm going to leave this question open for a few more days, if someone can come up with an answer, I'll happily accept it if its a working solution so that some others may be able to find this information useful at some point in the future.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如我在更新中提到的,我选择走不同的路线。相反,我想到在属性字符串中使用自定义属性来指定我的链接,因为无论如何我在创建时就有了它。所以我就这么做了。然后在我的触摸处理程序中,当点击运行时,我检查该运行是否具有该属性,如果是,则使用它调用我的委托。从那里我很乐意加载带有该 URL 的 Web 视图。
编辑:下面是解释我在这个答案中所做的事情的代码片段。享受。
然后...
So as I mentioned in the update, I elected to go a different route. Instead I got the idea to use a custom attribute in the attributed string to specify my link, since I had it at creation time anyway. So I did that. Then in my touch handler, when a run is tapped, I check if that run has that attribute, and if so, call my delegate with it. From there I'm happily loading a webview with that URL.
EDIT: Below are snippets of code explaining what I did in this answer. Enjoy.
then...
找到点击的行后,您可以通过调用
CTLineGetStringIndexForPosition()
来请求字符串中的索引。无需访问单独的运行。After you find the tapped line, you can ask for the index in string by calling
CTLineGetStringIndexForPosition()
. There's no need to access individual runs.