From 11f9642cba0cd41a7bd81162f68744eabf7940b6 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sat, 18 Jan 2020 19:32:39 -0500 Subject: [PATCH] Actually fix link interaction --- Tusker/Views/ContentTextView.swift | 42 +++++++++++++++++------------- 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/Tusker/Views/ContentTextView.swift b/Tusker/Views/ContentTextView.swift index 696c52f8..2a923fae 100644 --- a/Tusker/Views/ContentTextView.swift +++ b/Tusker/Views/ContentTextView.swift @@ -32,6 +32,10 @@ class ContentTextView: LinkTextView { textContainerInset = .zero textContainer.lineFragmentPadding = 0 + + // the text view's builtin link interaction code is tied to isSelectable, so we need to use our own tap recognizer + let recognizer = UITapGestureRecognizer(target: self, action: #selector(textTapped(_:))) + addGestureRecognizer(recognizer) } // MARK: - Emojis @@ -187,6 +191,15 @@ class ContentTextView: LinkTextView { } } + // only handles link taps via the gesture recognizer which is used when selection is disabled + @objc func textTapped(_ recognizer: UITapGestureRecognizer) { + let location = recognizer.location(in: self) + if let (link, range) = getLinkAtPoint(location) { + let text = (self.text as NSString).substring(with: range) + handleLinkTapped(url: link, text: text) + } + } + func getLinkAtPoint(_ point: CGPoint) -> (URL, NSRange)? { let locationInTextContainer = CGPoint(x: point.x - textContainerInset.left, y: point.y - textContainerInset.top) var partialFraction: CGFloat = 0 @@ -200,6 +213,16 @@ class ContentTextView: LinkTextView { return nil } + func handleLinkTapped(url: URL, text: String) { + if let mention = getMention(for: url, text: text) { + navigationDelegate?.selected(mention: mention) + } else if let tag = getHashtag(for: url, text: text) { + navigationDelegate?.selected(tag: tag) + } else { + navigationDelegate?.selected(url: url) + } + } + // MARK: - Navigation func getViewController(forLink url: URL, inRange range: NSRange) -> UIViewController { @@ -231,24 +254,7 @@ class ContentTextView: LinkTextView { extension ContentTextView: UITextViewDelegate { func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool { - let text = (self.text as NSString).substring(with: characterRange) - switch interaction { - case .invokeDefaultAction: - if let mention = getMention(for: URL, text: text) { - navigationDelegate?.selected(mention: mention) - } else if let tag = getHashtag(for: URL, text: text) { - navigationDelegate?.selected(tag: tag) - } else { - navigationDelegate?.selected(url: URL) - } - case .presentActions: - print("present actions") - case .preview: - print("preview") - @unknown default: - break - } - + // disable the text view's link interactions, we handle tapping links ourself with a gesture recognizer return false } }