mirror of
https://github.com/Ranchero-Software/NetNewsWire
synced 2026-12-01 18:10:16 +00:00
Create and use a CopyableLabel for the version string that appears in settings and about view.
This commit is contained in:
1 parent
5728e0a7b6
commit
b808eaad89
3 files changed
+90
-35
No files matched your search
@@ -0,0 +1,85 @@
|
||||
//
|
||||
// CopyableLabel.swift
|
||||
// NetNewsWire-iOS
|
||||
//
|
||||
// Created by Brent Simmons on 10/20/26.
|
||||
//
|
||||
|
||||
import UIKit
|
||||
import SwiftUI
|
||||
|
||||
/// A label that shows a Copy edit menu on long press. Copy copies the entire text — no selection.
|
||||
final class CopyableLabel: UILabel {
|
||||
|
||||
override init(frame: CGRect) {
|
||||
super.init(frame: frame)
|
||||
commonInit()
|
||||
}
|
||||
|
||||
required init?(coder: NSCoder) {
|
||||
super.init(coder: coder)
|
||||
commonInit()
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - UIEditMenuInteractionDelegate
|
||||
|
||||
extension CopyableLabel: @preconcurrency UIEditMenuInteractionDelegate {
|
||||
|
||||
func editMenuInteraction(_ interaction: UIEditMenuInteraction, menuFor configuration: UIEditMenuConfiguration, suggestedActions: [UIMenuElement]) -> UIMenu? {
|
||||
let title = NSLocalizedString("Copy", comment: "Command")
|
||||
let copyAction = UIAction(title: title) { [weak self] _ in
|
||||
UIPasteboard.general.string = self?.text
|
||||
}
|
||||
return UIMenu(children: [copyAction])
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Private
|
||||
|
||||
private extension CopyableLabel {
|
||||
|
||||
func commonInit() {
|
||||
isUserInteractionEnabled = true
|
||||
addInteraction(UIEditMenuInteraction(delegate: self))
|
||||
addGestureRecognizer(UILongPressGestureRecognizer(target: self, action: #selector(showEditMenu(_:))))
|
||||
}
|
||||
|
||||
@objc func showEditMenu(_ recognizer: UILongPressGestureRecognizer) {
|
||||
guard recognizer.state == .began else {
|
||||
return
|
||||
}
|
||||
guard let editMenuInteraction = interactions.first(where: { $0 is UIEditMenuInteraction }) as? UIEditMenuInteraction else {
|
||||
return
|
||||
}
|
||||
let sourcePoint = CGPoint(x: bounds.midX, y: bounds.minY)
|
||||
editMenuInteraction.presentEditMenu(with: UIEditMenuConfiguration(identifier: nil, sourcePoint: sourcePoint))
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - SwiftUI
|
||||
|
||||
/// SwiftUI wrapper for `CopyableLabel`: centered, secondary-colored body text.
|
||||
struct CopyableText: UIViewRepresentable {
|
||||
|
||||
let text: String
|
||||
|
||||
func makeUIView(context: Context) -> CopyableLabel {
|
||||
let label = CopyableLabel()
|
||||
label.font = UIFont.preferredFont(forTextStyle: .body)
|
||||
label.adjustsFontForContentSizeCategory = true
|
||||
label.textColor = UIColor.secondaryLabel
|
||||
label.textAlignment = .center
|
||||
label.numberOfLines = 0
|
||||
return label
|
||||
}
|
||||
|
||||
func updateUIView(_ label: CopyableLabel, context: Context) {
|
||||
label.text = text
|
||||
}
|
||||
|
||||
func sizeThatFits(_ proposal: ProposedViewSize, uiView label: CopyableLabel, context: Context) -> CGSize? {
|
||||
let width = proposal.width ?? CGFloat.greatestFiniteMagnitude
|
||||
return label.sizeThatFits(CGSize(width: width, height: CGFloat.greatestFiniteMagnitude))
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user