如何与使用 UIStringDrawing/CoreText 绘制的字符串进行交互

发布于 2024-09-12 23:37:00 字数 485 浏览 1 评论 0原文

我正在尝试开发一个突出显示某些关键词的应用程序。因此,我正在考虑使用 CoreText 或 UIStringDrawing 来突出显示这些单词(如粗体/下划线/斜体/颜色),因为 UITextView 似乎没有这些设施(而且我对 CATextLayer 没有任何运气) 。

在任何一种情况下,我都能够成功地操纵和绘制字符串。 (IE-我可以在字符串中找到重要的单词并应用适当的特征)。

我遇到的问题是,当以这种方式绘制文本时,它无法直接交互。鉴于用户将能够删除/添加文本到相关文本视图,这是一件很重要的事情。

目前,我已将绘图例程插入 UITextView 子类的 drawRect: 方法中(主要是为了看看我可以做我想做的事情)。 UITextView 是否有可以重写的不同方法,以便它使用我的代码来呈现其字符串?如果没有,我该如何去做我所描述的事情呢?

有关我正在谈论的内容的示例,请查看 Notes 应用程序如何查找并突出显示电话号码和地址。我不是在寻找电话号码或地址,但它表达了要点。

I am attempting to work on an app that highlights certain key words. For this reason, I am looking at using either CoreText or UIStringDrawing to highlight these words (As in bold/underline/italicize/color) since UITextView doesn't seem to have these facilities (And I haven't had any luck with CATextLayer).

In either case I am able to successfully manipulate and draw the string. (I.E- I can find my important words in a string and apply the appropriate traits).

The problem I am having is that when text is drawn this way, it cannot be interacted with directly. Seeing as the user will be able to delete/add text to the text view in question, this is an important thing to have.

At the moment I have stuck my drawing routines into the drawRect: method of a UITextView subclass (Mostly just to see that I can do what I want). Does UITextView have a different method that can be overridden so that it uses my code to render its string? If not, how might I go about doing what I've described?

For an example of what I am talking about, look at how the Notes app finds and highlights phone numbers and addresses. I'm not looking for phone numbers or addresses, but it gets the point across.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

遗弃M 2024-09-19 23:37:00

经过一些额外的阅读后,您似乎需要子类化/重写很多方法才能创建您自己的可以绘制文本的文本视图。 Apple 的文档对此有所涉及,但没有详细介绍。

After a bit of additional reading, it seems that you need to subclass/override a LOT of methods in order to create your own text view whose text you can draw. Apple's docs touch on it, but they don't go much into detail.

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