我相信这个问题以前已经被问过并得到了回答,但我找不到它。我看到有一个回答指向 Fancy Label 和 Three20,但它们并不完全符合我的要求,或者可能我错过了一些关键点。
基本上,我想获得应用程序用户的反馈,所以我会写在一个大标签中,比如
blah blah, 发送电子邮件至xxxxxx@gmail.com,还有更多的blah blah。
我希望电子邮件地址可点击,并打开电子邮件编辑器,以便用户可以编辑和发送。
这就是我需要的全部内容。如何实现?谢谢。
我相信这个问题以前已经被问过并得到了回答,但我找不到它。我看到有一个回答指向 Fancy Label 和 Three20,但它们并不完全符合我的要求,或者可能我错过了一些关键点。
基本上,我想获得应用程序用户的反馈,所以我会写在一个大标签中,比如
blah blah, 发送电子邮件至xxxxxx@gmail.com,还有更多的blah blah。
我希望电子邮件地址可点击,并打开电子邮件编辑器,以便用户可以编辑和发送。
这就是我需要的全部内容。如何实现?谢谢。
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];
要选择多个数据检测项:
myView.delegate = self;
- holierthanthou84这很简单,
在.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];
}
NSAttributedString
来实现相同的功能。使用这种类型的字符串可以节省很多时间。但在使用之前,你需要了解文本的确切位置。通过了解位置和长度,你可以轻松自定义该字符串。请点击此链接下载示例项目NSAttributed string。但如果你使用的是 iOS 6,则不需要使用此示例代码。因为在 iOS 6 中,UILabel 支持此类型的字符串。lbl.aatributText = @"Your NSAttributed string".
编辑: 以下是一些链接,您可以跟随: 1. 如何在UILabel的NSAttributedString中创建可点击的“链接”? 2. Objective-C UILabel作为超链接 3. 如何使文本中的特定单词具有可触摸的含义?