在 Xcode 中的字符串内向单词添加 TapRecognizer 和不同的颜色
我正在从 plist 中提取文本并将其绘制到 UIView 中。 该文本包含我想要突出显示并启用用户交互的术语,以便我可以弹出带有 teem 解释的字典视图。
我知道如何找到它们在绳子上的位置,效果很好。 但我没有找到如何让它们以不同的颜色出现。 对我来说更重要的是如何为它们添加点击识别器。
我将不胜感激任何帮助。
谢谢 沙尼
I am pulling a text from a plist and draw it into a UIView.
The text contains terms that I want to be highlighted and user interaction enabled so I could pop a dictionary view with the teem explanation.
I know how to find their location on the string and that works fine.
But I did not find how to make them a appear in different color.
And even more important to me - how to add a tap recognizer to them.
I would appreciate any help.
Thanks
Shani
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 UIView 中出现的单词前面添加一个具有相同字体和文本的透明按钮。我有一个类可以使用 UILabel 来完成此操作,如果您愿意的话?它产生与 Facebook 应用程序相同的效果。
Add a transparent button with the same font and text in front of where the word appears in the UIView. I have a class that will do this with a UILabel if you'd like it? It creates the same effect as with the Facebook application.
好的
我找到了一个非常适合我的解决方案。
我使用 UIWebView 作为文本视图,然后添加格式化为 HTML 的文本。
通过这种方式,我可以使用 CSS 更好地格式化文本,甚至将某些文本标记为链接,在示例中,我希望单词辅音将显示为链接。
(另一个很大的好处是我可以添加对 RTL 的支持)。
然后我使用这个函数来识别点击的单词并用字典文本午餐一个 UIView:
希望它能帮助别人。
沙尼
O.k
i found a solution that works great for me.
i am using UIWebView as The view for the text and then adds the text formatted as HTML.
in this way i can use CSS to format the text better and even tag some of the text as link ,in the example i want that the word consonant will appear as a link.
(an other great benefit is that I can add support to RTL languishes).
then i use this function to recognize what word clicks and lunch a UIView with the dictionary text:
hope it will help someone.
shani