UITextView 中简单链接的最佳方法

发布于 2024-08-03 00:28:19 字数 305 浏览 1 评论 0原文

我正在创建一个字典查找应用程序。用户从 UITableView 中选择一个单词,应用程序将显示其定义。在某些情况下,该单词与另一个单词相似,因此我想显示“另请参阅:”,然后显示相似单词的列表,当触摸这些单词时,会显示另一个定义。

在此处搜索 UITextViews 内的链接时,大多数答案都涉及链接到网络,这并不是我真正需要的。我只是想在用户触摸某个单词时获得控制权,以便我可以更改视图。

UIWebView 是执行此操作的唯一方法,还是我错过了 SDK 中明显的内容?另外,我更愿意留在本机 SDK 中,而不是走 Three20 路线。

谢谢!

I am creating a dictionary lookup application. The user selects a word from a UITableView and the app displays the definition. In some cases the word will be similar to another word, so I want to display "See Also:" followed by a list of similar words that when touched, bring up another definition.

In searching here on links within UITextViews, most of the answers involve linking out to the web, which is not really what I need. I simply want to get control when the user touches a word so that I can change the view.

Is UIWebView the only way to do this, or did I miss something obvious in the SDK? Also, I'd prefer to stay within the native SDK and not go the three20 route.

Thanks!

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

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

发布评论

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

评论(2

忘羡 2024-08-10 00:28:19

我将使用另一个 UITableView 来完成这项工作。您的相似单词列表可能已经采用 NSArray 格式,因此可以很容易地设置另一个 UITableView 而不是 UITextView 来显示列表,并且考虑到您已经有了适用于主 UITableView 的代码,您已经知道如何使它们可点击!

I would use another UITableView to make this work. Your list of similar words will probably be in NSArray format already, so it would be pretty easy to set up another UITableView instead of a UITextView to display the list, and given that you already have this code working for the main UITableView, you already know how to make them clickable!

行雁书 2024-08-10 00:28:19

恐怕 UIWebView 将是唯一适合这里的东西。数据检测器是在 UITextView 内部链接的唯一方法,它们只会响应适当的数据类型(电话号码、网页、地址)...

链接可以通过正常方式完成:

<a href='http://someotherword'>someotherword</a>

设置 webviewdelegate 来捕获任何链接请求(并阻止它们在浏览器中打开),以便您可以在自己的处理程序中打开它们:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request
{
  if(navigationType == UIWebViewNavigationTypeOther) return YES; // Allow direct loading

  NSString *myWord = [[request URL] host];
  // do something with myWord... say open another word
  return NO; // Don't let the browser actually perform this navigation
}

A UIWebView will be the only thing that suits here I'm afraid. Data detectors are the only way to link inside of a UITextView, and they will only respond to the appropriate data types (Phone number, web page, address)...

Links can be done the normal way:

<a href='http://someotherword'>someotherword</a>

Setup the webviewdelegate to snag any link requests (and prevent them from being opened in the browser) so that you can open them in your own handler:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request
{
  if(navigationType == UIWebViewNavigationTypeOther) return YES; // Allow direct loading

  NSString *myWord = [[request URL] host];
  // do something with myWord... say open another word
  return NO; // Don't let the browser actually perform this navigation
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文