From 5b3e7f5bae27c4915ff12376597733eb0ad573bc Mon Sep 17 00:00:00 2001 From: fish Date: Sun, 12 Jul 2026 21:14:18 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E5=8C=96=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E9=A1=B5=E9=9D=A2=E5=AF=BC=E8=88=AA=E6=A0=8F=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E7=B2=98=E8=B4=B4=E5=92=8C=E9=87=8D=E7=BD=AE=E6=8C=89=E9=92=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Note/FormatLogViewController.swift | 45 ++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/Note/FormatLogViewController.swift b/Note/FormatLogViewController.swift index adaed68..ca5e2cf 100644 --- a/Note/FormatLogViewController.swift +++ b/Note/FormatLogViewController.swift @@ -12,7 +12,15 @@ class FormatLogViewController: UIViewController { private var textView: UITextView! private var formatButton: UIButton! private var doneButton: UIBarButtonItem! + private var pasteButton: UIBarButtonItem! + private var resetButton: UIBarButtonItem! private var isFormatted = false + private var rightBarButtonMode: RightBarButtonMode = .paste + + private enum RightBarButtonMode { + case paste + case reset + } override func viewDidLoad() { super.viewDidLoad() @@ -33,6 +41,10 @@ class FormatLogViewController: UIViewController { view.addSubview(textView) 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.translatesAutoresizingMaskIntoConstraints = false @@ -68,6 +80,35 @@ class FormatLogViewController: UIViewController { textView.text = formatLog(textView.text) isFormatted = true 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 { func textViewDidBeginEditing(_ textView: UITextView) { - navigationItem.rightBarButtonItem = doneButton + updateRightBarButtonItem() } func textViewDidEndEditing(_ textView: UITextView) { - navigationItem.rightBarButtonItem = nil + updateRightBarButtonItem() } }