iOS swift: textView with links to another view

梦想的初衷 提交于 2019-12-13 03:58:13

问题


I'm trying to make a TextView with some test like this one:

Cras et enim ipsum. Nullam rhoncus euismod nisi at convallis. Etiam accumsan libero odio, eget malesuada ligula bibendum venenatis.

where the links bring you to an other ViewController... I think I should use attributed text to accomplish this, but I really can't figure how...

The other solution would be to use buttons surrounded by labels, but it is not elegant...

Helps?


回答1:


Use UITextView delegate method

textView(_:shouldInteractWith:in:interaction:)

You can check for URL in this method where you need to sort of deeplink to another viewcontroller. Also, return false from this method for such cases.

If the URLS can also be opened from outside the app, you may want to look at the Deeplink solutions available in iOS for better management and will give your users a deepklink functionality as well.




回答2:


You can use the active label frame work available i the github, as I use the same for achieving my goal and it is working fine
https://github.com/optonaut/ActiveLabel.swift
Example

func setupAgreement() {

        let customType1 = ActiveType.custom(pattern: "\\sterms of service\\b") //Looks for "are"
        let customType2 = ActiveType.custom(pattern: "\\sprivacy policy\\b") //Looks for "it"
        let customType3 = ActiveType.custom(pattern: "\\scookie use\\b") //Looks for "supports"

        userAgreementLabel.enabledTypes.append(customType1)
        userAgreementLabel.enabledTypes.append(customType2)
        userAgreementLabel.enabledTypes.append(customType3)

        userAgreementLabel.customize { (label) in

            label.text = "UserAgreement".localized
            label.numberOfLines = 0
            label.lineSpacing = 4
            label.textColor = UIColor(red: 0 / 255, green: 0 / 255, blue: 0 / 255, alpha: 1)

            //Custom types
            label.customColor[customType1] = Constant.AppColor.blueColor
            label.customSelectedColor[customType1] = Constant.AppColor.blueColor
            label.customColor[customType2] = Constant.AppColor.blueColor
            label.customSelectedColor[customType2] = Constant.AppColor.blueColor
            label.customColor[customType3] = Constant.AppColor.blueColor
            label.customSelectedColor[customType3] = Constant.AppColor.blueColor

            label.configureLinkAttribute = { (type, attributes, isSelected) in
                var atts = attributes
                switch type {
                case customType1:
                    atts[NSFontAttributeName] = UIFont(name: self.userAgreementLabel.font.fontName, size: 15.0)
                case customType2:
                    atts[NSFontAttributeName] = UIFont(name: self.userAgreementLabel.font.fontName, size: 15.0)
                case customType3:
                    atts[NSFontAttributeName] = UIFont(name: self.userAgreementLabel.font.fontName, size: 15.0)
                default: ()
                }

                return atts
            }

            label.handleCustomTap(for: customType1, handler: { (value) in
                self.load(cms: .terms)
            })
            label.handleCustomTap(for: customType2, handler: { (value) in
                 self.load(cms: .privacy)
            })
            label.handleCustomTap(for: customType3, handler: { (value) in
                self.load(cms: .cookie)
            })

        }
    }

    //  func navigateToWebView() {
    //      let controller = self.storyboard?.instantiateViewController(withIdentifier: Constant.StoryBoardIdentifier.webViewController)
    //      self.present(controller!, animated: true, completion: nil)
    //      
    //  }
    func load(cms: CMS) -> Void {
        let cmsController: CMSViewController = UIStoryboard(storyboard: .setting).initVC()
        cmsController.cms = cms
        show(cmsController, sender: cmsController.classForCoder)
    }



回答3:


You can check for the URL being pressed and present your view controller

func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool
{
    if let urlStr = request.url?.absoluteString
    {
        if (urlStr.lowercased().contains("yourURL"))
        {
            //go to your view controller
            return true
        }
    }
    return false
}


来源:https://stackoverflow.com/questions/46640352/ios-swift-textview-with-links-to-another-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!