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

10

我相信这个问题以前已经被问过并得到了回答,但我找不到它。我看到有一个回答指向 Fancy Label 和 Three20,但它们并不完全符合我的要求,或者可能我错过了一些关键点。

基本上,我想获得应用程序用户的反馈,所以我会写在一个大标签中,比如

blah blah, 发送电子邮件至xxxxxx@gmail.com,还有更多的blah blah。

我希望电子邮件地址可点击,并打开电子邮件编辑器,以便用户可以编辑和发送。

这就是我需要的全部内容。如何实现?谢谢。


3
使用UITextView,并将文本编辑设置为不可用。 - P.J
使用MFMailComposeViewController:https://dev59.com/questions/pGw05IYBdhLWcg3wszwl - jdb1a1
4个回答

9
你可以按照以下方式使用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


我认为你需要为它设置代理!myView.delegate = self; - holierthanthou84
@BaZinga 即使它是可点击的,如何检测到它被点击了?有什么想法吗? - Android Killer
你的实际需求是什么?需要详细说明吗? - KDeogharkar
如果我没有错的话,你想表达的是每当我们点击链接时,这个点击事件应该被处理到某个地方,对吗? - KDeogharkar
好的,你想在特定号码被点击时进行呼叫。这是你的需求吗?@AndroidKiller - KDeogharkar
显示剩余19条评论

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

请纠正我,但是这样做会使整个标签可点击,我认为他们只想要电子邮件地址可点击并且像HTML超链接一样呈蓝色(如他们所描述的)。 - Holyprin
是的,@Holyprin 这将使整个标签可点击,但标签的背景属性是 clearcolor,因此用户会感觉只有文本可点击,对于蓝色字体,他可以将文本颜色设置为蓝色。 - Dilip Manek
我没明白。标签不就是一个按钮吗?电子邮件地址将会在一个段落中。 - Tony Xu
nop,@TonyXu 你需要将用户交互属性设置为是,如果你想要文本居中,那么就将文本对齐属性设置为中心。如果你使用UILabel而不是UIButton,那么你可以对文本进行很多自定义操作。 - Dilip Manek
1
他的意思是使标签可交互,然后捕获包括句子中其他字词在内的整个标签的点击,并启动电子邮件应用程序。如果你想要这个功能,那么很简单。 - Holyprin
我认为您可以使用多个标签来实现这一点,Bazinga的答案很好,但它仅适用于链接。 - Dilip Manek

1
使用html / mailto:锚点的Webview应该可以很好地完成这个任务...可能需要将其样式设置为默认的iOS样式,但是没问题。

1
你可以使用 NSAttributedString 来实现相同的功能。使用这种类型的字符串可以节省很多时间。但在使用之前,你需要了解文本的确切位置。通过了解位置和长度,你可以轻松自定义该字符串。请点击此链接下载示例项目NSAttributed string。但如果你使用的是 iOS 6,则不需要使用此示例代码。因为在 iOS 6 中,UILabel 支持此类型的字符串。
lbl.aatributText = @"Your NSAttributed string".

编辑: 以下是一些链接,您可以跟随: 1. 如何在UILabel的NSAttributedString中创建可点击的“链接”? 2. Objective-C UILabel作为超链接 3. 如何使文本中的特定单词具有可触摸的含义?


1
属性字符串只能将电子邮件地址变成蓝色,但不能使其可点击,对吗? - Tony Xu
我不确定,但是通过查看此链接,我得到了一个想法,我们可以使用这个字符串来实现它。请参见讨论链接:http://www.cocoabuilder.com/archive/cocoa/241888-hyperlinks-in-nsattributedstring.html - aks.knit1108

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接