繁体   English   中英

UITextView带有可点击的链接或电话号码以及Swift中的自定义属性字符串

[英]UITextView with clickable link or phone number and custom attributed string in Swift

我正在开发一个iOS应用程序。 在我的应用程序中,我将在UITextView显示产品的详细信息。 详细信息可能包含电话号码,电子邮件地址或网站链接。 我可以使用UITextViews属性进行单击。 同样在我的UITextView ,我想在最后一个位置附加'Terms&Condition'字符串,当点击它时我想在弹出窗口中显示条款和条件。 多数民众赞成我也使用NSAttributed String。 但问题是当我点击URL,电话号码或我的自定义属性字符串时,它会显示弹出的术语。 如何区分我的点击次数,这意味着如果我点击它应该在浏览器中打开的网站,或者如果是电话号码,请询问我提示拨打该号码,如果是条款和条件,请打开弹出窗口。 请帮我。 我尝试了所有方法。 在iOS中可以吗?
这是我添加的代码

let str = "\(offer_desc) t&c"  //t&c is the clickbale string added to the actaul offer description

let attributedString = NSMutableAttributedString(string: str, attributes: [NSAttributedStringKey.font: UIFont(name: "Avenir-Medium", size: 12)])

let foundRange = attributedString.mutableString.range(of: "t&c") 
attributedString.addAttribute(NSAttributedStringKey.link, value: "", range: foundRange)

descriptionTextView.linkTextAttributes = [NSAttributedStringKey.foregroundColor.rawValue : UIColor.green]

descriptionTextView.attributedText = attributedString


func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange) -> Bool {

    self.showTermsCondition(self)
    return false
}

当我运行我的应用程序。 链接也是绿色,当点击打开术语弹出。

在此输入图像描述

首先,您想在一个textview中执行此操作吗? 如果没有,你可以使用stackview并将你的文本视图放入其中。 所以你可以通过手势识别器做你想做的事。

如果你想在一个文本视图上做,可能是你可以分享一个设计来更清楚地解决你的问题。

是的,我解决了我的问题。 我们需要做的是,像这样检查UITextView的回调中的URL。

func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange) -> Bool {
    if URL.absoluteString == ""  { //
       self.showTermsCondition(self)
       return false
    }

    return true //System will take care according to URL type, weblink, email or phone number
}

或者你也可以这样做,

@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
}

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)

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM