Just how to you use TTStyledTextLabel?
Asked Answered
O

2

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?

Osteotomy answered 23/7, 2009 at 14:16 Comment(1)
Why you don't use UIWebView? When you click link in webView, UIWebViewDelegate get shouldStartLoadWithRequest messageTrophic
A
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.

Assignor answered 23/7, 2009 at 15:10 Comment(7)
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?Osteotomy
I've updated my answer to point you in the right direction for finding out how the URL mapping stuff works.Assignor
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 clickedOsteotomy
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.Assignor
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).Locoweed
Hey, any answers to my query?Philippa
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 <a href=\"google.com\">work</a>"; label.text = [TTStyledText textFromXHTML:labelText lineBreaks:NO URLs:YES]; [self.view addSubview:label]; Should this workSmock
G
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];
}
Garnettgarnette answered 30/1, 2013 at 6:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.