2017-10-09 44 views
0

我試圖讓一個TextView有像這樣的一些測試:iOS的SWIFT:TextView的鏈接到其他視圖

信用評級機構等enim存有。 Nullam rhoncus euismod nisi在convallis。 Etiam accumsan libero odio,eget malesuada ligula bibendum venenatis。

其中的鏈接帶給你的其他的ViewController ...我想我應該用屬性文本來做到這一點,但我真想不通怎麼...

其他的解決辦法是使用標籤包圍的按鈕,但它不是優雅的...

有幫助嗎?

+0

看到此HTTPS:/ /stackoverflow.com/questions/995219/how-to-make-uitextview-detect-links-for-website-mail-and-phone-number –

+0

nop,與我試圖訪問網絡的鏈接.. 。但只是到另一個視圖 –

+0

使用'shouldInteractWith'並處理您的鏈接 –

回答

1

使用UITextView委託方法

textView(_:shouldInteractWith:in:interaction:)

您可以在此方法中,你需要理清的深層鏈接到另一個視圖 - 控制檢查URL。此外,對於這種情況,請從此方法返回false。

如果還可以從應用程序外部打開URLS,則可能需要查看iOS中提供的Deeplink解決方案以獲得更好的管理,並且還會爲用戶提供深入鏈接功能。

+0

的檢測,以及如何添加從uistoryboard的鏈接? –

+0

@TheMiotz:鏈接可以添加到屬性字符串中。看[這裏](https://stackoverflow.com/questions/21629784/how-to-make-a-clickable-link-in-an-nsattributedstring-for-a)。但我不知道如何在故事板/筆尖中完成。 –

0

您可以檢查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 
} 
0

您可以使用現有的有源標籤框架的工作,我在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) 
    }