Highlight link touches

This commit is contained in:
Justin Mazzocchi 2020-09-29 23:00:04 -07:00
parent 175bd9f78e
commit 834264ebc6
No known key found for this signature in database
GPG Key ID: E223E6937AAFB01C
1 changed files with 44 additions and 1 deletions

View File

@ -5,6 +5,8 @@ import UIKit
final class TouchFallthroughTextView: UITextView { final class TouchFallthroughTextView: UITextView {
var shouldFallthrough: Bool = true var shouldFallthrough: Bool = true
private var linkHighlightView: UIView?
override init(frame: CGRect, textContainer: NSTextContainer?) { override init(frame: CGRect, textContainer: NSTextContainer?) {
super.init(frame: frame, textContainer: textContainer) super.init(frame: frame, textContainer: textContainer)
initializationActions() initializationActions()
@ -19,6 +21,35 @@ final class TouchFallthroughTextView: UITextView {
shouldFallthrough ? urlAndRect(at: point) != nil : super.point(inside: point, with: event) shouldFallthrough ? urlAndRect(at: point) != nil : super.point(inside: point, with: event)
} }
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
guard let touch = touches.first,
let (_, rect) = urlAndRect(at: touch.location(in: self)) else {
return
}
let linkHighlightView = UIView(frame: rect)
self.linkHighlightView = linkHighlightView
linkHighlightView.transform = Self.linkHighlightViewTransform
linkHighlightView.layer.cornerRadius = .defaultCornerRadius
linkHighlightView.backgroundColor = .secondarySystemBackground
insertSubview(linkHighlightView, at: 0)
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesEnded(touches, with: event)
removeLinkHighlightView()
}
override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesCancelled(touches, with: event)
removeLinkHighlightView()
}
override var selectedTextRange: UITextRange? { override var selectedTextRange: UITextRange? {
get { shouldFallthrough ? nil : super.selectedTextRange } get { shouldFallthrough ? nil : super.selectedTextRange }
set { set {
@ -91,10 +122,22 @@ final class TouchFallthroughTextView: UITextView {
} }
private extension TouchFallthroughTextView { private extension TouchFallthroughTextView {
private func initializationActions() { static let linkHighlightViewTransform = CGAffineTransform(scaleX: 1.1, y: 1.1)
func initializationActions() {
clipsToBounds = false
textDragInteraction?.isEnabled = false textDragInteraction?.isEnabled = false
textContainerInset = .zero textContainerInset = .zero
textContainer.lineFragmentPadding = 0 textContainer.lineFragmentPadding = 0
linkTextAttributes = [.foregroundColor: tintColor as Any, .underlineColor: UIColor.clear] linkTextAttributes = [.foregroundColor: tintColor as Any, .underlineColor: UIColor.clear]
} }
func removeLinkHighlightView() {
UIView.animate(withDuration: .defaultAnimationDuration) {
self.linkHighlightView?.alpha = 0
} completion: { _ in
self.linkHighlightView?.removeFromSuperview()
self.linkHighlightView = nil
}
}
} }