iOS如何使标签中的电子邮件地址可点击

时间:2013-02-16 05:04:27

标签: ios email

我相信这个问题之前已被提出并得到解答,但我找不到。我看到一个答案指向Fancy Label和Three20,但它们并不是我想要的,或者我可能错过了一些观点。

基本上,我想得到应用程序用户的反馈,所以我会写一个大标签,比如等等等等,给我发电子邮件xxxxxx@gmail.com,等等等等。我希望电子邮件地址可以点击,并打开电子邮件编辑器,以便用户可以编辑和发送。

这就是我所需要的。怎么弄?感谢。

4 个答案:

答案 0 :(得分:8)

您可以按如下方式使用UITextView:

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];

选择多个数据检测:

enter image description here

答案 1 :(得分:2)

这很简单,

在.h文件中创建标签插座

@interface ContactUsViewController : UIViewController<MFMailComposeViewControllerDelegate>

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

并将此代码放在.m文件

- (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];
}

答案 2 :(得分:1)

Webview使用html / mailto:锚应该可以正常工作...可能需要将其设置为默认的iOS内容,但是是的。

答案 3 :(得分:1)

您可以使用NSAttributedString。使用这种类型的字符串将节省大量时间。但在使用之前,您必须知道文本的确切位置。通过了解位置和长度,您可以轻松自定义该字符串。请参阅此链接以下载示例项目。NSAttributed string。但是,如果您使用的是ios 6,则无需使用此示例代码。您可以直接使用NSAttributedString。因为在ios6中,UILabel支持这种类型的字符串。

lbl.aatributText = @"Your NSAttributed string".

修改 以下是您可以关注的一些链接: 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?