Best approach for simple link in UITextView
I am creating a dictionary search application. The user selects a word from the UITableView and the application displays the definition. In some cases, a word will look like another word, so I want to display "See Also:" and then a list of similar words that trigger a different definition when touched.
Searching here for links in UITextViews, most of the answers are about web binding, which is actually not what I need. I just want to get control when the user touches the word to change the view.
Is UIWebView the only way to do this, or am I missing something obvious in the SDK? Also, I would rather stay in the native SDK than go down route three20.
Thank!
I would use another one UITableView
to make this work. Your list of similar words will probably already be in the format NSArray
, so it would be pretty easy to set up another one UITableView
instead UITextView
to display the list, and given that you already have this code working for main UITableView
, you already know how to make them available to viewing!
UIWebView will be the only one that suits me, I'm afraid. Data detectors are the only way to bind inside a UITextView and they will only respond to the relevant data types (phone number, web page, address) ...
Links can be done in the usual way:
<a href='http://someotherword'>someotherword</a>
Set up the webviewdelegate to catch any communication requests (and prevent them from opening in the browser) so 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
}