UILabel - string as text and links

IphoneIosNsstringUilabel

Iphone Problem Overview


I have a UILabel whose text I am getting from a server. Some of the text is to be identified as links, and on touching those links some action should be performed. e.g.

NSString *str = @"My phone number is 645-345-2345 and my address is xyz";

This is the complete text for UILabel. I have only one UILabel for displaying this text (Text is dynamic. I just gave an example.). On clicking these links I need to perform actions like navigating to some different screen or make a call.
I know that I can display such text with help of OHAttributedLabel. And the links can be displayed as follows :

[label1 addCustomLink:[NSURL URLWithString:@"http://www.foodreporter.net"] inRange:[txt rangeOfString:someString]];  

But I wonder how can I make these text links perform some action like navigation to different screen or making a call.
Let me know if more explanation is required.

Iphone Solutions


Solution 1 - Iphone

You can add custom actions to any of the available UILabel replacements that support links using a fake URL scheme that you'll intercept later:

TTTAttributedLabel *tttLabel = <# create the label here #>;
NSString *labelText = @"Lost? Learn more.";
tttLabel.text = labelText;
NSRange r = [labelText rangeOfString:@"Learn more"]; 
[tttLabel addLinkToURL:[NSURL URLWithString:@"action://show-help"] withRange:r];

Then, in your TTTAttributedLabelDelegate:

- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url {
    if ([[url scheme] hasPrefix:@"action"]) {
        if ([[url host] hasPrefix:@"show-help"]) {
            /* load help screen */
        } else if ([[url host] hasPrefix:@"show-settings"]) {
            /* load settings screen */
        }
    } else {
        /* deal with http links here */
    }
}

TTTAttributedLabel is a fork of OHAttributedLabel.

If you want a more complex approach, have a look to Nimbus Attributed Label. It support custom links out-of-the-box.

Solution 2 - Iphone

You can use UITextView with Phone numbers and links detection YES, scrolling disabled YES user interaction enabled YES, instead of UILabel.

Solution 3 - Iphone

My project has successfully used OHAttributedLabel for this. Check out the

-(BOOL)attributedLabel:(OHAttributedLabel*)attributedLabel shouldFollowLink:(NSTextCheckingResult*)linkInfo;

method in OHAttributedLabelDelegate (link). It allows you to decide what happens when a link is clicked. If you look at the source for the example from the OHAttributedLabel project, it's used to display an alert. If you returned NO in this case (to keep the default action from happening too), you could just do whatever you wanted like navigation, etc.

Note however that this requires that you can determine the action correctly just from the text. For our project, we used a slightly fancier solution, where the server sent us text with tags in them and a list of commands to perform for each tag.

Solution 4 - Iphone

There a project called FancyLabel that is about what you need. It might need some customization though. Also, I think Three20 has this functionality, but it might be an overkill if you don't already use it.

There's also a much simpler solution, if all of your links are phones \ addresses \ urls. You can simply use a UITextView instead of a UILabel. It has auto detection of phones, address, etc. (just check the boxes in IB)

You can also have custom actions in response to click events on those links by overriding openURL, as explained here

Is there a specific reason that you must use a UILabel instead of a UITextView? Note that a lot of the implementations of attributed labels inherit from UIView or don't implement all of UILabel's functionality.

Solution 5 - Iphone

You can use custom button to give a look like of link ..Also you can add gesture on the custom label if you dont want to use button ..

UITapGestureRecognizer* gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(userTappedOnLink:)];
// if labelView is not set userInteractionEnabled, you must do so
[labelView setUserInteractionEnabled:YES];
[labelView addGestureRecognizer:gesture];

Solution 6 - Iphone

I'm not a fan of being forced to use UITextView or a third party lib when all I need is a lightweight label that renders links (and that tells me when they're tapped!)

Here's my attempt at a lightweight UILabel subclass able to detect link taps. The approach is different from others I've seen in that it gains access to the UILabel's shared NSLayoutManager via a delegate callback added to NSTextStorage via a category extension. The beauty is that UILabel performs its native layout and drawing - other UILabel replacements often augment or replace the native behavior with an additional NSLayoutManager/NSTextContainer.

Probably App Store safe, but somewhat fragile - use at your own risk!

https://github.com/TomSwift/TSLabel

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNitishView Question on Stackoverflow
Solution 1 - IphonedjromeroView Answer on Stackoverflow
Solution 2 - IphoneGaneshView Answer on Stackoverflow
Solution 3 - IphoneMartin GjaldbaekView Answer on Stackoverflow
Solution 4 - IphoneadamsitonView Answer on Stackoverflow
Solution 5 - IphoneAli3nView Answer on Stackoverflow
Solution 6 - IphoneTomSwiftView Answer on Stackoverflow