Facebook-style "hyperlinks" in text
Its bee开发者_开发百科n 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
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.
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.
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
精彩评论