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?
view.addSubview(textView!)at the end of yourlayoutTextView()function. This will add your text view to the View Hierarchy, allowing it to be displayed.textViewoptional variable to an instance of your custom class inside viewDidLoad, and then callinglayoutTextView()inside viewDidLayoutSubviews?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.