diff --git a/Tusker/Views/ContentTextView.swift b/Tusker/Views/ContentTextView.swift index b0f6cb80..ea700587 100644 --- a/Tusker/Views/ContentTextView.swift +++ b/Tusker/Views/ContentTextView.swift @@ -33,6 +33,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 @@ -188,6 +192,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 @@ -201,6 +214,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 { @@ -232,24 +255,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 } }