Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS how to make email address in a label clickable

Tags:

email

ios

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.

like image 929
Tony Xu Avatar asked Feb 16 '13 05:02

Tony Xu


4 Answers

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

like image 96
KDeogharkar Avatar answered Oct 14 '22 11:10

KDeogharkar


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];
}
like image 29
Dilip Avatar answered Oct 14 '22 12:10

Dilip


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

like image 43
Holyprin Avatar answered Oct 14 '22 10:10

Holyprin


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?

like image 32
aks.knit1108 Avatar answered Oct 14 '22 10:10

aks.knit1108