1

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?

3
  • You've created the text view and formatted it, but never actually added it to the superview, so it isn't displayed. To fix this, try adding view.addSubview(textView!) at the end of your layoutTextView() function. This will add your text view to the View Hierarchy, allowing it to be displayed. Commented Jun 29, 2022 at 3:54
  • I'd assume you're assigning the textView optional variable to an instance of your custom class inside viewDidLoad, and then calling layoutTextView() inside viewDidLayoutSubviews? Commented Jun 29, 2022 at 3:56
  • Have you tried temporarily replacing your custom text view with a default UITextView (leaving everything else the same) and seeing if it displays the link for you? I tested it with a default UITextView and it was displaying fine, as @VoidAsif has confirmed in the answer. If this fixes the problem, it might be something to do with the custom text view implementation. Commented Jun 29, 2022 at 20:55

1 Answer 1

3

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.