Bark/View/MessageList/CustomTapTextView.swift

81 lines
2.8 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// CustomTapTextView.swift
// Bark
//
// Created by huangfeng on 12/30/24.
// Copyright © 2024 Fin. All rights reserved.
//
import UIKit
/// UITextView UITextView
/// TextView
class CustomTapTextView: UITextView, UIGestureRecognizerDelegate {
///
private lazy var tapGesture = UITapGestureRecognizer(target: self, action: #selector(tap))
/// tapGesture
private let doubleTapGesture = UITapGestureRecognizer()
/// UITextView
private var linkTapGesture: UIGestureRecognizer? = nil
///
var customTapAction: (() -> Void)?
init() {
super.init(frame: .zero, textContainer: nil)
self.backgroundColor = UIColor.clear
self.isEditable = false
self.dataDetectorTypes = [.phoneNumber, .link]
self.isScrollEnabled = false
self.textContainerInset = .zero
self.textContainer.lineFragmentPadding = 0
tapGesture.delegate = self
self.addGestureRecognizer(tapGesture)
self.linkTapGesture = self.gestureRecognizers?.first { $0 is UITapGestureRecognizer && $0.name == "UITextInteractionNameLinkTap" }
doubleTapGesture.numberOfTapsRequired = 2
doubleTapGesture.delegate = self
self.addGestureRecognizer(doubleTapGesture)
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
@objc func tap() {
self.customTapAction?()
}
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
if gestureRecognizer == doubleTapGesture {
return true
}
return false
}
override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
if gestureRecognizer == tapGesture {
if self.selectedRange.length > 0 {
return false
}
}
return super.gestureRecognizerShouldBegin(gestureRecognizer)
}
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRequireFailureOf otherGestureRecognizer: UIGestureRecognizer) -> Bool {
if gestureRecognizer == tapGesture {
if otherGestureRecognizer == doubleTapGesture {
return true
}
if otherGestureRecognizer == linkTapGesture {
return true
}
}
return false
}
}