格式化日志页面导航栏添加粘贴和重置按钮

This commit is contained in:
2026-07-12 21:14:18 +08:00
parent 76c3509118
commit 5b3e7f5bae
+43 -2
View File
@@ -12,7 +12,15 @@ class FormatLogViewController: UIViewController {
private var textView: UITextView! private var textView: UITextView!
private var formatButton: UIButton! private var formatButton: UIButton!
private var doneButton: UIBarButtonItem! private var doneButton: UIBarButtonItem!
private var pasteButton: UIBarButtonItem!
private var resetButton: UIBarButtonItem!
private var isFormatted = false private var isFormatted = false
private var rightBarButtonMode: RightBarButtonMode = .paste
private enum RightBarButtonMode {
case paste
case reset
}
override func viewDidLoad() { override func viewDidLoad() {
super.viewDidLoad() super.viewDidLoad()
@@ -33,6 +41,10 @@ class FormatLogViewController: UIViewController {
view.addSubview(textView) view.addSubview(textView)
doneButton = UIBarButtonItem(title: "完成", style: .prominent, target: self, action: #selector(didTapDoneButton)) doneButton = UIBarButtonItem(title: "完成", style: .prominent, target: self, action: #selector(didTapDoneButton))
pasteButton = UIBarButtonItem(title: "粘贴文案", style: .plain, target: self, action: #selector(didTapPasteButton))
resetButton = UIBarButtonItem(title: "重置", style: .plain, target: self, action: #selector(didTapResetButton))
updateRightBarButtonItem()
formatButton = UIButton(type: .system) formatButton = UIButton(type: .system)
formatButton.translatesAutoresizingMaskIntoConstraints = false formatButton.translatesAutoresizingMaskIntoConstraints = false
@@ -68,6 +80,35 @@ class FormatLogViewController: UIViewController {
textView.text = formatLog(textView.text) textView.text = formatLog(textView.text)
isFormatted = true isFormatted = true
formatButton.setTitle("保存到 iCloud", for: .normal) formatButton.setTitle("保存到 iCloud", for: .normal)
rightBarButtonMode = .reset
updateRightBarButtonItem()
}
}
@objc private func didTapPasteButton() {
if let string = UIPasteboard.general.string {
textView.text = string
}
}
@objc private func didTapResetButton() {
textView.text = ""
isFormatted = false
formatButton.setTitle("格式化", for: .normal)
rightBarButtonMode = .paste
updateRightBarButtonItem()
}
private func updateRightBarButtonItem() {
if textView.isFirstResponder {
navigationItem.rightBarButtonItem = doneButton
} else {
switch rightBarButtonMode {
case .paste:
navigationItem.rightBarButtonItem = pasteButton
case .reset:
navigationItem.rightBarButtonItem = resetButton
}
} }
} }
@@ -124,11 +165,11 @@ class FormatLogViewController: UIViewController {
extension FormatLogViewController: UITextViewDelegate { extension FormatLogViewController: UITextViewDelegate {
func textViewDidBeginEditing(_ textView: UITextView) { func textViewDidBeginEditing(_ textView: UITextView) {
navigationItem.rightBarButtonItem = doneButton updateRightBarButtonItem()
} }
func textViewDidEndEditing(_ textView: UITextView) { func textViewDidEndEditing(_ textView: UITextView) {
navigationItem.rightBarButtonItem = nil updateRightBarButtonItem()
} }
} }