How to display clickable links in UITextView

后端 未结 3 653
轮回少年
轮回少年 2020-12-01 12:36

I am trying to display an attributed string in a UITextview with clickable links. I\'ve created a simple test project to see where I\'m going wrong and still can\'t figure i

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-01 13:23

    For swift3.0

      override func viewDidLoad() {
         super.viewDidLoad()
    
      let linkAttributes = [
            NSLinkAttributeName: NSURL(string: "http://stalwartitsolution.co.in/luminutri_flow/terms-condition")!
            ] as [String : Any]
      let attributedString = NSMutableAttributedString(string: "Please tick box to confirm you agree to our Terms & Conditions, Privacy Policy, Disclaimer. ")
    
      attributedString.setAttributes(linkAttributes, range: NSMakeRange(44, 18))
    
      attributedString.addAttribute(NSUnderlineStyleAttributeName, value: NSNumber(value: 1), range: NSMakeRange(44, 18))
    
      textview.delegate = self
      textview.attributedText = attributedString
      textview.linkTextAttributes = [NSForegroundColorAttributeName: UIColor.red]
      textview.textColor = UIColor.white
      }
    
    
      func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
        return true
       }
    

提交回复
热议问题