【问题标题】:iOS how to make email address in a label clickableiOS如何使标签中的电子邮件地址可点击
【发布时间】:2013-02-16 05:04:26
【问题描述】:

我相信以前有人问过并回答过这个问题,但我找不到。我看到一个指向 Fancy Label 和 Three20 的答案,但它们并不是我想要的,或者我可能错过了一些要点。

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

等等等等,给我发电子邮件到 xxxxxx@gmail.com,等等等等。

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

这就是我所需要的。如何得到它?谢谢。

【问题讨论】:

标签: ios email


【解决方案1】:

您可以按如下方式使用 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];

选择多个数据检测:

【讨论】:

  • 只有我一个人吗?刚才stackoverflow似乎下降了一段时间。无论如何,@BaZinga,使用你的方法,我看不到文本 google.com 变得可点击。我需要将 textview 委托放在 .h 文件中吗?
  • 我正在动态创建文本视图,您也可以通过将控件放到视图中来创建它,这对我来说很好。
  • 把它放在你的 viewdidload 方法中只是为了检查
  • 我现在看到 google.com 的蓝色字体,但它不可点击。如何为其添加方法?另外,如何制作邮件链接? mailto 似乎不起作用....谢谢。
  • 您现在不想添加任何其他方法。只需点击它,它就可以工作。
【解决方案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 这将使整个标签可点击,但标签的背景属性是 clearcolor 所以用户填充就像只有文本是可点击的,对于蓝色字体,他可以将文本颜色设置为蓝色。
  • 我没听懂。标签不只是一个按钮吗?电子邮件地址将在一个段落中。
  • nop,@TonyXu 你必须将用户交互属性设置为是,如果你想让文本居中,然后将文本对齐属性设置为中心,我不理解平均段落。如果你使用 UILabel 而不是 UIButton比你可以用文本做很多自定义。
  • 他的意思是让标签可以交互,然后点击整个标签,包括句子中的其他词,然后启动电子邮件应用程序。如果你想要那个功能就足够简单了。
【解决方案3】:

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

【讨论】:

    【解决方案4】:

    您也可以使用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?

    【讨论】:

    • 属性字符串只会使电子邮件地址变为蓝色,但不可点击,对吧?
    • 我不确定,但是通过看到这个链接,我知道我们可以通过使用这个字符串来做到这一点。见讨论链接。 cocoabuilder.com/archive/cocoa/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-23
    • 1970-01-01
    • 2013-02-20
    • 2011-05-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多