views:

76

answers:

3

Hi there.

Its been bugging me for absolutely weeks now, but I can't find out how to get links in bodies of text like in the facebook iphone app.

The specific word that is a link is a different color to the rest of the text and when clicked changes color and has a gray box around it.

The only way I can think is with a UIWebView - but I'm pretty positive that that's not the way facebook does it.

Any ideas? - i would need the text to disclose to another view - so it would need to run a method if possible.

Thank you

A: 

On Mac Os you can embed a hyperlink using an NSAttributedString. The resultant attributed string will contain the url, style and color for display.

On iphone to render attributed text you would have to use CoreText ( iOS 3.2 and above ). Probably UIWebView would be better since its out of the box and doesnt require 3.2.

Cheers, Krzysztof Zabłocki

Krzysztof Zabłocki
+2  A: 

Hello Thomas,

You can check out Three20 library, which is exactly what sits inside of facebook. You can find the exact solution they're using inside.

However, some people find Three20 a bit overheaded, since you have to import most of the library for the sole label. That's why I personally prefer FancyUILabel from Craig Hockenberry who worked on Twitterrific - you can find it here.

Hope this helps, Pawel

Pawel
that library looks brilliant, however I (as is a lot of people I think, judging by the comments) am having an incredibly tough time trying to implement it. Shame, because it looks absolutely fantastic. Will have a look at fancyUILabel. :)
Thomas Clayson
FancyUILabel - its a bit hacky, but it does the trick! :)
Thomas Clayson
+1  A: 

I have done same in one of my iPhone application. I have used UIWebView. Set delegate to self. Whatever line you want to show as link put it in ancor tag . Load your html content in webView. Then implement below delegate method

- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType 
{
if (navigationType == UIWebViewNavigationTypeLinkClicked)
{
     NSURL *URL = [request URL];
    // Write your code here
}
}

When you will touch on link you will get url (which you have given in href attribute) inside callback method. So you can perform whatever action you want. To show selection on touch you can use javascript in your html content. It works great.

Avinash
this is fine but I didn't want to use this - I wanted to put the links into a UILabel really - I don't want the "bounce" and the ugly shadowed gutter that comes with the bog standard uiwebview. Thanks though.
Thomas Clayson