iOS可点击单词(UILabel或UIbutton的) [英] iOS Clickable Words (UILabel or UIbutton's)
问题描述
我想在我的应用程序中创建一个只读文本区域,该区域允许用户单击任何单词,然后应用程序将其读出.但是,我对哪种方法最好是有些困惑.我看到两个选项,使用UILabel并创建一些方法来检测单击的区域,然后将其与该区域中的单词匹配,但这听起来很难实现.另一方面,我可以使用单词数组来创建UIbutton的列表.感谢Jason,我们将不胜感激任何建议和/或示例代码.
I'm wanting to create a read only text area in my app which allows the user to click on any word and the app reads it out. I am however a little confused on which method would be the best. I see two options, use a UILabel and create some method to detect the region clicked then match it to the word in that region but it sounds hard to implement. On the other hand I could use an array of words to create a list of UIbutton's. Any advice and/or sample code to help me would be much appreciated, thanks Jason.
注意:每个视图上约有30个字.
Note: Each view has about 30 words on it.
以下解决方案效果很好.对于其他想要使用此功能的人,这四行代码会将您的UIWebView设置为背景清晰,并禁止任何滚动或跳动.
The solution below works well. For anyone else wanting to use this, these four lines will set your UIWebView to have a clear background and disable any scrolling or bounce.
[[myWebView.subviews objectAtIndex:0] setScrollEnabled:NO];
[[myWebView.subviews objectAtIndex:0] setBounces:NO];
[myWebView setBackgroundColor:[UIColor clearColor]];
[myWebView setOpaque:NO];
还有一些方便的CSS,可以在用户按住链接时停止打开弹出窗口.
And some handy css to stop the open popup when a user presses and holds a link.
*{-webkit-touch-callout:none; -webkit-user-select: none;}
推荐答案
您的文本区域有多大?如果很大,则为每个作品创建一个UIButton听起来有点费劲,以使文本正确地进行布局.
How big is your text area? If it's big then creating a UIButton for each work sounds like sa bit of effor to get the text to layout correctly.
我将使用UIWebView-使每个单词都像这样:
I would use a UIWebView - make each word like this :
<a href="wordpress://WORD1">WORD1</a> <a href="wordpress://WORD2">WORD2</a> <a href="wordpress://WORD3">WORD3</a>
并将您的视图控制器附加为webView的 UIWebViewDelegate委托
.
and attach your view controller as the webView's UIWebViewDelegate delegate
.
然后,您可以使用 webView:shouldStartLoadWithRequest:navigationType:
委托方法:)
Then, you can intercept presses on each word using the webView:shouldStartLoadWithRequest:navigationType:
delegate method :)
这篇关于iOS可点击单词(UILabel或UIbutton的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!