【问题标题】:iOS swift: textView with links to another viewiOS swift:带有指向另一个视图的链接的文本视图
【发布时间】:2018-03-20 07:16:43
【问题描述】:

我正在尝试使用类似这样的测试来制作一个 TextView:

Cras et enim ipsum。 Nullam rhoncus euismod nisi 在康瓦利斯。 Etiam accumsan libero odio, eget malesuada ligula bibendum 黄花菜。

链接将您带到另一个 ViewController... 我想我应该使用属性文本来完成此操作,但我真的不知道如何...

另一种解决方案是使用被标签包围的按钮,但这并不优雅......

有帮助吗?

【问题讨论】:

标签: ios swift textview nsattributedstring


【解决方案1】:

使用UITextView委托方法

textView(_:shouldInteractWith:in:interaction:)

您可以在此方法中检查 URL,您需要对另一个视图控制器进行深层链接。此外,在这种情况下,从该方法返回 false。

如果 URL 也可以从应用程序外部打开,您可能需要查看 iOS 中可用的 Deeplink 解决方案,以便更好地管理,并为您的用户提供 deepklink 功能。

【讨论】:

  • 如何从 uistoryboard 添加链接?
  • @TheMiotz:可以在属性字符串中添加链接。看here。但我不知道如何在故事板/笔尖中完成。
【解决方案2】:

您可以检查被按下的 URL 并显示您的视图控制器

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
}

【讨论】:

    【解决方案3】:

    您可以使用 github 上可用的活动标签框架,因为我使用它来实现我的目标并且它工作正常
    https://github.com/optonaut/ActiveLabel.swift
    示例

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-28
      • 2018-05-30
      • 1970-01-01
      • 2011-11-03
      • 2014-12-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多