9

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

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

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

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

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

4

4 回答 4

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

选择多个数据检测:

在此处输入图像描述

于 2013-02-16T05:35:10.810 回答
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];
}
于 2013-02-16T05:15:52.890 回答
1

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

lbl.aatributText = @"Your NSAttributed string".

编辑: 以下是您可以关注的一些链接: 1.在 UILabel 的 NSAttributedString 中创建可点击的“链接”? 2. Objective-C UILabel as HyperLink 3.如何让一个特定的词在文本中的意义可以触摸?

于 2013-02-16T05:54:46.303 回答
1

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

于 2013-02-16T05:06:06.777 回答