mirror of
https://github.com/Ranchero-Software/NetNewsWire
synced 2025-08-12 06:26:36 +00:00
Fix several warnings.
This commit is contained in:
@@ -9,33 +9,38 @@
|
||||
import UIKit
|
||||
|
||||
@IBDesignable
|
||||
class InteractiveLabel: UILabel {
|
||||
class InteractiveLabel: UILabel, UIEditMenuInteractionDelegate {
|
||||
|
||||
override init(frame: CGRect) {
|
||||
super.init(frame: frame)
|
||||
commonInit()
|
||||
}
|
||||
|
||||
|
||||
required init?(coder: NSCoder) {
|
||||
super.init(coder: coder)
|
||||
commonInit()
|
||||
}
|
||||
|
||||
|
||||
func commonInit() {
|
||||
let gestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPressGesture(_:)))
|
||||
self.addGestureRecognizer(gestureRecognizer)
|
||||
|
||||
let editMenuInteraction = UIEditMenuInteraction(delegate: self)
|
||||
addInteraction(editMenuInteraction)
|
||||
|
||||
self.isUserInteractionEnabled = true
|
||||
}
|
||||
|
||||
@objc func handleLongPressGesture(_ recognizer: UIGestureRecognizer) {
|
||||
guard recognizer.state == .began,
|
||||
let recognizerView = recognizer.view,
|
||||
let recognizerSuperView = recognizerView.superview,
|
||||
recognizerView.becomeFirstResponder() else {
|
||||
return
|
||||
guard recognizer.state == .began, let recognizerView = recognizer.view else {
|
||||
return
|
||||
}
|
||||
|
||||
if let interaction = recognizerView.interactions.first(where: { $0 is UIEditMenuInteraction }) as? UIEditMenuInteraction {
|
||||
let location = recognizer.location(in: recognizerView)
|
||||
let editMenuConfiguration = UIEditMenuConfiguration(identifier: nil, sourcePoint: location)
|
||||
interaction.presentEditMenu(with: editMenuConfiguration)
|
||||
}
|
||||
|
||||
UIMenuController.shared.showMenu(from: recognizerSuperView, rect: recognizerView.frame)
|
||||
}
|
||||
|
||||
override var canBecomeFirstResponder: Bool {
|
||||
@@ -44,11 +49,20 @@ class InteractiveLabel: UILabel {
|
||||
|
||||
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
|
||||
return (action == #selector(UIResponderStandardEditActions.copy(_:)))
|
||||
|
||||
}
|
||||
|
||||
|
||||
override func copy(_ sender: Any?) {
|
||||
UIPasteboard.general.string = text
|
||||
}
|
||||
|
||||
|
||||
// MARK: - UIEditMenuInteractionDelegate
|
||||
|
||||
func editMenuInteraction(_ interaction: UIEditMenuInteraction, menuFor configuration: UIEditMenuConfiguration, suggestedActions: [UIMenuElement]) -> UIMenu? {
|
||||
|
||||
let copyAction = UIAction(title: "Copy", image: nil) { [weak self] action in
|
||||
self?.copy(nil)
|
||||
}
|
||||
return UIMenu(title: "", children: [copyAction])
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user