iOS how to make email address in a label clickable

10,410

Solution 1

You could use UITextView as follows:

UITextView *myView = [[UITextView alloc] initWithFrame: CGRectMake(0, 0, 300, 50)];
    myView.text = @"this is http://google.com link";
    myView.editable = NO;
    myView.dataDetectorTypes = UIDataDetectorTypeLink;    
    //myView.message.dataDetectorTypes = UIDataDetectorTypePhoneNumber|UIDataDetectorTypeLink; for multiple data detection
    [self.view addSubview:myView];
    [myView release];

to select more than one data detection :

enter image description here

Solution 2

This is pretty simple,

create a label outlet in .h file

@interface ContactUsViewController : UIViewController<MFMailComposeViewControllerDelegate>

@property (strong, nonatomic) IBOutlet UILabel *mail1Lbl;

and place this code in.m file

- (void)viewDidLoad
{
    [super viewDidLoad];

    UITapGestureRecognizer* mail1LblGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(mail1LblTapped:)];
    // if labelView is not set userInteractionEnabled, you must do so
[mail1Lbl setText:@"[email protected]"];
    [mail1Lbl setUserInteractionEnabled:YES];
    [mail1Lbl addGestureRecognizer:mail1LblGesture];
}

- (void)mail1LblTapped:(id)sender
{
    if ([MFMailComposeViewController canSendMail])
    {

        MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
        mailer.mailComposeDelegate = self;
        [mailer setSubject:@""];
        NSArray *toRecipients = [NSArray arrayWithObjects:@"[email protected]", nil];
        [mailer setToRecipients:toRecipients];
        NSString *emailBody = @"";
        [mailer setMessageBody:emailBody isHTML:NO];
        mailer.navigationBar.barStyle = UIBarStyleBlackOpaque;
        [self presentModalViewController:mailer animated:YES];

    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Failure"
                                                        message:@"Your device doesn't support the composer sheet"
                                                       delegate:nil
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [alert show];
    }
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
    switch (result)
    {
        case MFMailComposeResultCancelled:
            NSLog(@"Mail cancelled: you cancelled the operation and no email message was queued.");
            break;
        case MFMailComposeResultSaved:
            NSLog(@"Mail saved: you saved the email message in the drafts folder.");
            break;
        case MFMailComposeResultSent:
            NSLog(@"Mail send: the email message is queued in the outbox. It is ready to send.");
            break;
        case MFMailComposeResultFailed:
            NSLog(@"Mail failed: the email message was not saved or queued, possibly due to an error.");
            break;
        default:
            NSLog(@"Mail not sent.");
            break;
    }
    // Remove the mail view
    [self dismissModalViewControllerAnimated:YES];
}

Solution 3

Webview using html / mailto: anchor should work just fine for this... may need to style it to the default iOS stuff but yeah.

Solution 4

You can use NSAttributedString for the same. Using this type of string will save a lot of time. But before using this you have to knowledge exact position of text. By knowing position and length you can customize that string easily. See this link for downloading sample project.NSAttributed string. But if you are using ios 6 then no need to use this sample code. You can use directly NSAttributedString. Because in ios6 UILabel supports this type of string.

lbl.aatributText = @"Your NSAttributed string".

Edit: Here are some links which you can follow: 1. Create tap-able "links" in the NSAttributedString of a UILabel? 2. Objective-C UILabel as HyperLink 3. how to make a specific word touchable for its meaning in a text?

Share:
10,410
Tony Xu
Author by

Tony Xu

Updated on July 24, 2022

Comments

  • Tony Xu
    Tony Xu almost 2 years

    I believe this question has been asked and answered before, but I couldn't find it. I see an answer pointing to Fancy Label and Three20, but they are not quite what I want, or probably I missed some points.

    Basically, I want get app users' feedback, so I will write in a big label, like

    blah blah, email me at [email protected], and blah blah more.

    I want the email address clickable, and open email composer so that users can edit and send.

    That's all I need. How to get it? Thanks.

  • Holyprin
    Holyprin over 11 years
    Correct me if I'm wrong, but doing it this way makes the ENTIRE label clickable, I think they only want the email address clickable and blue like an html hyperlink (so they described.)
  • Dilip
    Dilip over 11 years
    yes,@Holyprin this will make whole label clickable ,but label's background property is clearcolor so user fill like only text are clickable and for blue font he can set text color to blue.
  • Tony Xu
    Tony Xu over 11 years
    I didn't get it. Isn't label just a button? The email address will be in a paragraph.
  • Dilip
    Dilip over 11 years
    nop, @TonyXu you have to set user interaction property to yes ,and i dont understand by mean paragraph if you want text to middle then set text align property to center.if you use UILabel instead of UIButton than you can do lot customization with text.
  • Holyprin
    Holyprin over 11 years
    What he's saying is to make the label interact-able, then catch the click on the entire label including the other words in the sentence and launch the email application. Simple enough if you want that functionality.
  • Dilip
    Dilip over 11 years
    i think you can achieve this using more than one label bazinga's ans is good but it only work for link.
  • Tony Xu
    Tony Xu over 11 years
    Was it just me? stackoverflow seemed down for a while just now. Anyway, @BaZinga, use your method, I don't see the text google.com become clickable. Do I need put textview delegate in the .h file?
  • Tony Xu
    Tony Xu over 11 years
    attributed string only makes the email address blue, but not clickable, right?
  • KDeogharkar
    KDeogharkar over 11 years
    i am creating the textview dynamically you can create by putting the control to your view also and it's work fine to me.
  • KDeogharkar
    KDeogharkar over 11 years
    put it in your viewdidload method just for checking
  • aks.knit1108
    aks.knit1108 over 11 years
    I am not sure but by seeing this link, I got a idea that we can do it by using this string. See discussion link. cocoabuilder.com/archive/cocoa/…
  • Tony Xu
    Tony Xu over 11 years
    I now see blue font for google.com, but it is not clickable. How do I add a method to it? In addition, how to make email link? mailto seems not working.... Thanks.
  • KDeogharkar
    KDeogharkar over 11 years
    you don't want to add any additional method now. Just click on it and it works.
  • holierthanthou84
    holierthanthou84 about 11 years
    i think you need to set the delegate for it !! myView.delegate = self;
  • Android Killer
    Android Killer almost 11 years
    @BaZinga even if it clickable how to detect that click ? any idea ?
  • KDeogharkar
    KDeogharkar almost 11 years
    Dont get it @AndroidKiller
  • KDeogharkar
    KDeogharkar almost 11 years
    what's your actual need. should you elaborate?
  • KDeogharkar
    KDeogharkar almost 11 years
    if I am not wrong you are trying to say that whenever we click the link that click event should be handle some where, Right?
  • Android Killer
    Android Killer almost 11 years
    @BaZinga absolutely right. I am trying to add one number into the uitextview and detect it.But while clicking it should be detected si that i can make a call to that number.
  • KDeogharkar
    KDeogharkar almost 11 years
    ohk you want to call at particular number whenever it get clicked. is that your need @AndroidKiller
  • Android Killer
    Android Killer almost 11 years
    @BaZinga thr is a long text, inside that there is number and emails. I want when i will the number then i can call that number and similarly for email when i will click mail then i can be able to send mail to that mail id. Hope it is clear.
  • KDeogharkar
    KDeogharkar almost 11 years
    see my edited answer @AndroidKiller. I dont try with more than one detection but as there is multiple selection it will defently work :)
  • Android Killer
    Android Killer almost 11 years
    @BaZinga that i did man... how to detect means when i will click it should call that number. got my point ?
  • KDeogharkar
    KDeogharkar almost 11 years
    have you try on device? @AndroidKiller
  • Android Killer
    Android Killer almost 11 years
    @BaZinga no can anyhow i can try on simulator ?
  • KDeogharkar
    KDeogharkar almost 11 years
    than try it on device first. the link will open in simulator but for calling number you have to check it in device . than you get the idea @AndroidKiller
  • Android Killer
    Android Killer almost 11 years
    @BaZinga ok and what about email, will it open email box ?
  • KDeogharkar
    KDeogharkar almost 11 years
    ok I tested it in simulator and it give me 2 option 1)add to contant and 2) copy
  • KDeogharkar
    KDeogharkar almost 11 years
    on long press of email id i get 3 option 1)new message - to mail to that address 2) new contact - to save that address 3) copy - to copy that address
  • KDeogharkar
    KDeogharkar almost 11 years
    But as I tell you @AndroidKiller please check all this for real device .
  • Android Killer
    Android Killer almost 11 years
    ok +1 for helping me. i am actually from android background.So i don't know much.
  • KDeogharkar
    KDeogharkar almost 11 years
    my pleasure . I only use link detection so I also learn about behavior for other data detection.