mirror of https://github.com/oxen-io/session-ios
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
76 lines
2.6 KiB
Swift
76 lines
2.6 KiB
Swift
// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
|
|
|
|
import UIKit
|
|
|
|
// Requirements:
|
|
// • Links should show up properly and be tappable
|
|
// • Text should * not * be selectable (this is handled via the 'textViewDidChangeSelection(_:)'
|
|
// delegate method)
|
|
// • The long press interaction that shows the context menu should still work
|
|
final class BodyTextView: UITextView {
|
|
private let snDelegate: BodyTextViewDelegate?
|
|
private let highlightedMentionBackgroundView: HighlightMentionBackgroundView = HighlightMentionBackgroundView()
|
|
|
|
override var attributedText: NSAttributedString! {
|
|
didSet {
|
|
guard attributedText != nil else { return }
|
|
|
|
highlightedMentionBackgroundView.maxPadding = highlightedMentionBackgroundView
|
|
.calculateMaxPadding(for: attributedText)
|
|
highlightedMentionBackgroundView.frame = self.bounds.insetBy(
|
|
dx: -highlightedMentionBackgroundView.maxPadding,
|
|
dy: -highlightedMentionBackgroundView.maxPadding
|
|
)
|
|
}
|
|
}
|
|
|
|
init(snDelegate: BodyTextViewDelegate?) {
|
|
self.snDelegate = snDelegate
|
|
|
|
super.init(frame: CGRect.zero, textContainer: nil)
|
|
|
|
self.clipsToBounds = false // Needed for the 'HighlightMentionBackgroundView'
|
|
addSubview(highlightedMentionBackgroundView)
|
|
|
|
setUpGestureRecognizers()
|
|
}
|
|
|
|
override init(frame: CGRect, textContainer: NSTextContainer?) {
|
|
preconditionFailure("Use init(snDelegate:) instead.")
|
|
}
|
|
|
|
required init?(coder: NSCoder) {
|
|
preconditionFailure("Use init(snDelegate:) instead.")
|
|
}
|
|
|
|
private func setUpGestureRecognizers() {
|
|
let longPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress))
|
|
addGestureRecognizer(longPressGestureRecognizer)
|
|
let doubleTapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleDoubleTap))
|
|
doubleTapGestureRecognizer.numberOfTapsRequired = 2
|
|
addGestureRecognizer(doubleTapGestureRecognizer)
|
|
}
|
|
|
|
@objc private func handleLongPress() {
|
|
snDelegate?.handleLongPress()
|
|
}
|
|
|
|
@objc private func handleDoubleTap() {
|
|
// Do nothing
|
|
}
|
|
|
|
override func layoutSubviews() {
|
|
super.layoutSubviews()
|
|
|
|
highlightedMentionBackgroundView.frame = self.bounds.insetBy(
|
|
dx: -highlightedMentionBackgroundView.maxPadding,
|
|
dy: -highlightedMentionBackgroundView.maxPadding
|
|
)
|
|
}
|
|
}
|
|
|
|
protocol BodyTextViewDelegate {
|
|
|
|
func handleLongPress()
|
|
}
|