10

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 xxxxxx@gmail.com, 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.

Bruno Bieri
  • 9,724
  • 11
  • 63
  • 92
Tony Xu
  • 3,031
  • 4
  • 32
  • 43
  • 3
    use UITextView and make text editable to false.. – P.J Feb 16 '13 at 05:05
  • Use MFMailComposeViewController: http://stackoverflow.com/questions/7087199/xcode-4-ios-send-an-email-using-smtp-from-inside-my-app – jdb1a1 Feb 16 '13 at 05:10

4 Answers4

9

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

KDeogharkar
  • 10,939
  • 7
  • 51
  • 95
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:@"xxxxxx@gmail.com"];
    [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:@"xxxxxx@gmail.com", 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];
}
Dilip Manek
  • 9,095
  • 5
  • 44
  • 56
  • 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.) – Holyprin Feb 16 '13 at 05:19
  • 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. – Dilip Manek Feb 16 '13 at 05:21
  • I didn't get it. Isn't label just a button? The email address will be in a paragraph. – Tony Xu Feb 16 '13 at 05:26
  • 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. – Dilip Manek Feb 16 '13 at 05:32
  • 1
    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. – Holyprin Feb 16 '13 at 05:35
  • i think you can achieve this using more than one label bazinga's ans is good but it only work for link. – Dilip Manek Feb 16 '13 at 05:50
1

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

Holyprin
  • 348
  • 1
  • 9
1

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?

Community
  • 1
  • 1
aks.knit1108
  • 1,305
  • 9
  • 20
  • 1
    attributed string only makes the email address blue, but not clickable, right? – Tony Xu Feb 16 '13 at 06:41
  • 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. http://www.cocoabuilder.com/archive/cocoa/241888-hyperlinks-in-nsattributedstring.html – aks.knit1108 Feb 16 '13 at 08:51