如何使用可点击的超链接呈现文本,您可以在其中控制单击操作? [英] How to render text with clickable hyperlinks where you can control the action for click?
问题描述
我正在尝试在我的应用中显示一些文本中可能有一些网址的文字。我希望将URL突出显示为超链接并可单击。此外,单击它时,我想提供一个回调,我可以执行自定义操作。当然URL应该传回我的回调,所以我知道如何处理它(比如在应用内浏览器中打开它等)。
I'm tryiing to display some text in my app where there might be some URL in the text. I want the URL to be highlighted as hyperlinks and clickable. Furthermore, when it is clicked, I want to supply a callback where I can perform custom action. Of course the URL should be passed back to my callback so I know what to do with it (say open it in an in-app browser etc).
这可能听起来很复杂,但基本上appstore中的所有IM应用程序(超过30个)都具有此功能,用户可以发送包含URL和接收的IM end可以单击它并在应用程序内浏览器中打开页面。
This might sound complicated but basically all the IM apps in the appstore (over 30 of them) have this functionality where a user can send an IM containing a URL and the receiving end can click on it and open the page in an in-app browser.
这是怎么做到的?我只是想不出来。如果您有解决方案,请提供示例代码。谢谢。
How is this done? I just can't figure it out. If you have a solution, please provide sample code. Thank you.
BTW:
答案是不是UIWebView (因为渲染它的多个实例的速度非常慢)。
The answer is NOT A UIWebView (because it is very slow to render multiple instances of it).
答案也是 NOT UITextView ,因为虽然它能够解析URL但却无法设置点击自定义回调。所以它最终打开了safari。
The answer is also NOT A UITextView because although it is capable of parsing URL it cannot be set a custom callback on clicks. So it just ends up opening safari.
答案也是 NOT TTStyledTextLabel ,因为它虽然能够解析URL并分配自定义动作对它来说,它需要在URLMap中预先分配。显然,在这种情况下,我们不知道URL是什么。据我所知,您不能将所有网址分配给自定义操作。
The answer is also NOT A TTStyledTextLabel because although it is capable of parsing URL and assigning a custom action to it, it needs to be pre-assigned in a URLMap. And obviously in this case we don't know what the URL is before hand. As far as I know you cannot assign ALL url to a custom action.
替代文字http://img169.yfrog.com/img169/3373/picture1ufx.png
推荐答案
据我所知,您不能将所有网址分配给自定义操作。
As far as I know you cannot assign ALL url to a custom action.
我在你的其他SO问题中给了你指示,所以我'有点疑惑,为什么你没有阅读我链接的源,因为它回答了这个完全的问题。 TTURLMap
通过 -from:toViewController:selector
方法支持catch-alls。例如:
I gave you directions in your other SO question, so I'm a bit puzzled as to why you haven't read the source I linked to because it answered this exact question. TTURLMap
supports catch-alls via the "-from:toViewController:selector
" method. For example:
[map from:@"*" toViewController:[ViewController class]
selector:@selector(someHandler:)];
如果你正在处理一个完整的无证件库,我会理解这种混淆,但Three20库是非常有充分的记录,包括样本应用程序,它们完全符合您的要求。
I'd understand the confusion if you were dealing with a complete undocumented library, but the Three20 library is very well documented to the point of including sample applications that do exactly what you're after.
这篇关于如何使用可点击的超链接呈现文本,您可以在其中控制单击操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!