简体   繁体   中英

Clickable link not displaying with programmatically created custom UITextView in swift

I want to get hyperlink in UITextView and with tap gesture I'm enabling textView.isEditable = true. But the link is not displaying in UITextView. I have a custom TextView which I'm calling in ViewController.

Here's my code:

 var textView: TextView?

override public func viewDidLoad() {
    super.viewDidLoad()
 
    textView = TextView(
      editorConfig: EditorConfig(
        theme: getTheme()
      )
    )
  }
 override public func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    view.backgroundColor = .white

    layoutTextView()
}
private func layoutTextView() {
    textView?.frame = CGRect(
      x: view.safeAreaInsets.left,
      y: view.safeAreaInsets.top,
      width: view.bounds.width - view.safeAreaInsets.left - view.safeAreaInsets.right,
      height: view.frame.size.height / 3
    )

    textView?.layer.borderWidth = 1
    textView?.layer.borderColor = UIColor.gray.cgColor
    textView?.isEditable = false
    textView?.isUserInteractionEnabled = true
    textView?.textColor = .black
    textView?.dataDetectorTypes = .link
    textView?.text = "https://www.google.com"
    
    let tap = UITapGestureRecognizer(target: self, action: #selector(textViewTapped))
    textView?.addGestureRecognizer(tap)
  }

@objc func textViewTapped(_ aRecognizer: UITapGestureRecognizer) {
    textView?.dataDetectorTypes = []
      textView?.isEditable = true
      textView?.becomeFirstResponder()
  }
  
  func textViewDidEndEditing(_ textView: UITextView) {
      textView.isEditable = false
      textView.dataDetectorTypes = .all
  }

private func addAllSubviews() {
    if let textView = textView {
      view.addSubview(textView)
    }
}

What am I missing?

Please provide all information regarding your code as you are using custom class of UITextview.

Declaration of textview i did as

var textView = UITextView()

I tested your code with Default UITextview and as @Quack E. Duck says you just need to add below code and you are done.

view.addSubview(textView)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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