iphoneobjective-cthree20ttstyledtextlabel

URLs and Images in TTStyledText


Hi i created a TTStyledTextLabel, works well.

Now i want to make URL clickable, i've done this:

textLabel.text = [TTStyledText textFromXHTML:[content objectForKey:@"content"] lineBreaks:YES URLs:YES];

but my links are still unclickable. Do i have to setup a UIWebView first? Where to declare it?

Another thing is, is it possible to resize Images inside my TTStyledTextLabel to fit them to the Label Size?

Thanks in advance

// edit

what i have exactly done:

textLabel = [[TTStyledTextLabel alloc] initWithFrame:CGRectMake(0.0f, 70.0f, 320.0f, 297.0f)];
textLabel.contentInset = UIEdgeInsetsMake(20, 15, 20, 15);
textLabel.font = [UIFont systemFontOfSize:14];
textLabel.text = [TTStyledText textFromXHTML:[content objectForKey:@"content"] lineBreaks:YES URLs:YES];
[textLabel sizeToFit];
//CGFloat height = textLabel.height;
[scrollView addSubview:textLabel];
scrollView.contentSize = textLabel.frame.size;

an NSLog of my [content objectForKey:@"content"] returns something like this:

<a href="http://www.abc.com/">Download-Link</a>

My Links are highlighted in my label, but they are unclickable.

i initialized my textlabel in - (void)viewDidLoad in a UIViewController


Solution

  • the [content objectForKey:@"content"] should return data containing <a href="url">string to display</a>
    

    if you'll add the url to TTURLMap it will also open the relevant controller

    the following code snippet should work

    self = [super init];
    TTStyledTextLabel* label = [[[TTStyledTextLabel alloc]   initWithFrame:CGRectMake(0, 0, 320, 230)] autorelease];
    label.text = [TTStyledText textFromXHTML:@"<a href=\"aa://link1\">link</a> text" lineBreaks:YES URLs:YES];
    [label setFont:[UIFont systemFontOfSize:16]];
    [[self view] addSubview:label];
    

    //edit

    So you probably need to map "*" in the URLMap if you are using the TTNavigator, something like:

    TTNavigator* navigator = [TTNavigator navigator];
    
    navigator.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [navigator.window makeKeyAndVisible];
    
    TTURLMap* map = navigator.URLMap;
    [map from:@"*" toViewController:[TTWebController class]];
    

    this will map all of the urls to the TTWebController which will open a webview to browse in