我有一个UITextView对象.UIView中的文字有电话号码,邮件链接,网站链接.我想将它们显示为具有以下功能的链接.
当有人点击URL时 - Safari应该打开网站.当有人点击电子邮件链接时 - 邮件应该打开我的地址到字段当有人点击电话号码时 - 电话应用程序应拨打该号码
有没有人以前做过这个或知道如何处理它?
谢谢,AJ
如果您使用的是OS3.0
你可以像下面这样做
textview.editable = NO; textview.dataDetectorTypes = UIDataDetectorTypeAll;
有关检测电子邮件地址的注意事项:必须安装邮件应用程序(它不在iOS模拟器上),用于打开邮件撰写屏幕的电子邮件链接.
从swift 3.0开始,如果您想以编程方式执行此操作,请使用以下代码.
textview.isEditable = false textview.dataDetectorTypes = .all
或者如果你有故事板
虽然问题是超级老。如果有人遇到同样的问题
也可以用作UILabel。尽管下面的解决方案可以完成工作:[不需要任何库..]
因此,我使用了MFMailcomposer()和UITexView [代码在Swift 3.0-Xcode 8.3.2中]
100%的崩溃证明和工作代码可处理所有极端情况。= D
步骤1。
import MessageUI
步骤2.添加委托
class ViewController: UITextViewDelegate, MFMailComposeViewControllerDelegate{
步骤3.从StoryBoard添加textView IBOutlet
@IBOutlet weak var infoTextView: UITextView!
步骤4.在您的viewDidload()中调用以下方法
func addInfoToTextView() { let attributedString = NSMutableAttributedString(string: "For further info call us on : \(phoneNumber)\nor mail us at : \(email)") attributedString.addAttribute(NSLinkAttributeName, value: "tel://", range: NSRange(location: 30, length: 10)) attributedString.addAttribute(NSLinkAttributeName, value: "mailto:", range: NSRange(location: 57, length: 18)) self.infoTextView.attributedText = attributedString self.infoTextView.linkTextAttributes = [NSForegroundColorAttributeName:UIColor.blue, NSUnderlineStyleAttributeName:NSNumber(value: 0)] self.infoTextView.textColor = .white self.infoTextView.textAlignment = .center self.infoTextView.isEditable = false self.infoTextView.dataDetectorTypes = UIDataDetectorTypes.all self.infoTextView.delegate = self }
步骤5.为TextView实现委托方法
@available(iOS, deprecated: 10.0) func textView(_ textView: UITextView, shouldInteractWith url: URL, in characterRange: NSRange) -> Bool { if (url.scheme?.contains("mailto"))! && characterRange.location > 55{ openMFMail() } if (url.scheme?.contains("tel"))! && (characterRange.location > 29 && characterRange.location < 39){ callNumber() } return false } //For iOS 10 @available(iOS 10.0, *) func textView(_ textView: UITextView, shouldInteractWith url: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool { if (url.scheme?.contains("mailto"))! && characterRange.location > 55{ openMFMail() } if (url.scheme?.contains("tel"))! && (characterRange.location > 29 && characterRange.location < 39){ callNumber() } return false }
步骤6.编写帮助程序方法以打开MailComposer和Call App
func callNumber() { if let phoneCallURL = URL(string: "tel://\(phoneNumber)") { let application:UIApplication = UIApplication.shared if (application.canOpenURL(phoneCallURL)) { let alert = UIAlertController(title: "Call", message: "\(phoneNumber)", preferredStyle: UIAlertControllerStyle.alert) if #available(iOS 10.0, *) { alert.addAction(UIAlertAction(title: "Call", style: .cancel, handler: { (UIAlertAction) in application.open(phoneCallURL, options: [:], completionHandler: nil) })) } else { alert.addAction(UIAlertAction(title: "Call", style: .cancel, handler: { (UIAlertAction) in application.openURL(phoneCallURL) })) } alert.addAction(UIAlertAction(title: "cancel", style: .default, handler: nil)) self.present(alert, animated: true, completion: nil) } } else { self.showAlert("Couldn't", message: "Call, cannot open Phone Screen") } } func openMFMail(){ let mailComposer = MFMailComposeViewController() mailComposer.mailComposeDelegate = self mailComposer.setToRecipients(["\(email)"]) mailComposer.setSubject("Subject..") mailComposer.setMessageBody("Please share your problem.", isHTML: false) present(mailComposer, animated: true, completion: nil) }
步骤7.编写MFMailComposer的Delegate方法
func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) { switch result { case .cancelled: print("Mail cancelled") case .saved: print("Mail saved") case .sent: print("Mail sent") case .failed: print("Mail sent failure: \(String(describing: error?.localizedDescription))") default: break } controller.dismiss(animated: true, completion: nil) }
就这样,您就完成了... = D
这是上面代码的swift文件: textViewWithEmailAndPhone.swift
设置以下属性以将其用作UILabel