7

All I want is to display some simple text in my viewController and have the hyperlinks autoparsed. When the user clicks on the link I want the control to somehow do a callback where I can do something with the URL. How can I achieve this?

I've already looked through the TTCatalog for hours. I have also tried looking into the source code of three20 as well as looking at the stack trace. No help. I just can't figure out how my app can react to the click of the URL. Any hints please?

erotsppa
  • 14,248
  • 33
  • 123
  • 181
  • 1
    Why you don't use UIWebView? When you click link in webView, UIWebViewDelegate get shouldStartLoadWithRequest message – oxigen Jul 23 '09 at 14:31

2 Answers2

11

Hard to help without seeing what you've already tried, but you should be able to do something like the following:

TTStyledTextLabel* label = [[[TTStyledTextLabel alloc] 
        initWithFrame:someFrame] autorelease];
NSString* labelText = @"This should <a href=\"custom-uri://some/url\">work</a>";
label.text = [TTStyledText textFromXHTML:labelText lineBreaks:NO URLs:YES];
[someView addSubview:label];

You can then use TTNavigator and TTURLMap to map custom-uri://some/url to a particular controller in your application, or handle it yourself in your application delegate. The best place to find out how to do that is by looking at the TTNavigatorDemo sample application included in the Three20 source. Specifically, look at AppDelegate.m which is where all the URL mapping gets performed.

Nathan de Vries
  • 15,481
  • 4
  • 49
  • 55
  • Great start! Can you give a brief example of how to use TTNavigator and TTURLMap to say push a controller when the URL is clicked? – erotsppa Jul 23 '09 at 16:23
  • I've updated my answer to point you in the right direction for finding out how the URL mapping stuff works. – Nathan de Vries Jul 24 '09 at 06:00
  • This url mapping system wouldn't work for this question, correct? It seems like the url have to be preset by the developer, and does not work with text that are given by the user. I want the TTStyledTextLabel to display some text from the user, and have all the hyperlinks parsed and perform a single same action when clicked – erotsppa Jul 24 '09 at 14:03
  • Your question has zero details about how the text is actually entered or what the URLs are, so it's impossible for me to know whether or not you can do what you want. You can map any URL, so it's likely that you can do what you're after, but you're going to need to read the sample code like I told you in my answer. – Nathan de Vries Jul 25 '09 at 06:33
  • 1
    The one other element that might be missing from this answers is that, when the link in the label invokes the `TTNavigator`, it will also ask its delegate whether the URL should be opened. So that's a way to listen for arbitrary links, without specifically mapping their destinations (as is done with the in-app navigation demonstrated in the TTNavigatorDemo). – Sixten Otto Oct 06 '09 at 05:30
  • Hey, any answers to my query? – Raj Pawan Gumdal Jun 16 '10 at 08:43
  • I have been trying to get the TTStyledTextLabel to work following the example in the TTCatalog demo with no luck. In fact I get no text at l? I then tried this sample here, but I dont see any blue hypertext in the label but I do see the plain text? I have tried this: TTStyledTextLabel* label = [[[TTStyledTextLabel alloc] initWithFrame:CGRectMake(5, 0, 315, 175)] autorelease]; NSString* labelText = @"This should work"; label.text = [TTStyledText textFromXHTML:labelText lineBreaks:NO URLs:YES]; [self.view addSubview:label]; Should this work – user7865437 Jan 28 '11 at 09:00
0

In addition to what Nathan says about URL mapping and links, you can also use CSS styles!

TTStyledTextLabel* label = [[[TTStyledTextLabel alloc] initWithFrame:someFrame] autorelease];
NSString* labelText = @"This should <a href=\"custom-uri://some/url\">work</a> and 
<span class=\"redText\">this should be red</span>";
label.text = [TTStyledText textFromXHTML:labelText lineBreaks:NO URLs:YES];
[someView addSubview:label];

Then in your StyleSheet.m implement

- (TTStyle*) redText {
  return [TTTextStyle styleWithFont:[UIFont systemFontOfSize:12] color:RGBCOLOR(255,0,0) next:nil];
}
eanticev
  • 948
  • 6
  • 10