iOS Clickable Words (UILabel or UIbutton's)

你。 提交于 2019-12-13 08:42:26

问题


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.

Note: Each view has about 30 words on it.

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]; 

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;} 

回答1:


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.

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>

and attach your view controller as the webView's UIWebViewDelegate delegate.

Then, you can intercept presses on each word using the webView:shouldStartLoadWithRequest:navigationType: delegate method :)



来源:https://stackoverflow.com/questions/7078789/ios-clickable-words-uilabel-or-uibuttons

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!