From 51fb757143631c922dbe93d57b8d2c9da8097329 Mon Sep 17 00:00:00 2001 From: fish Date: Mon, 13 Jul 2026 21:30:10 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=94=AE=E7=9B=98=E5=BC=B9?= =?UTF-8?q?=E8=B5=B7=E6=97=B6=E9=81=AE=E6=8C=A1=E5=BA=95=E9=83=A8=E6=96=87?= =?UTF-8?q?=E6=9C=AC=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Note/FormatLogViewController.swift | 33 +++++++++++++++++++++++++++++ Note/FormatNoteViewController.swift | 33 +++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/Note/FormatLogViewController.swift b/Note/FormatLogViewController.swift index ff480b0..74ab6e5 100644 --- a/Note/FormatLogViewController.swift +++ b/Note/FormatLogViewController.swift @@ -28,6 +28,11 @@ class FormatLogViewController: UIViewController { navigationItem.largeTitleDisplayMode = .never view.backgroundColor = .systemGroupedBackground setupUI() + setupKeyboardObservers() + } + + deinit { + NotificationCenter.default.removeObserver(self) } private func setupUI() { @@ -77,6 +82,34 @@ class FormatLogViewController: UIViewController { textView.resignFirstResponder() } + private func setupKeyboardObservers() { + NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: UIResponder.keyboardWillShowNotification, object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: UIResponder.keyboardWillHideNotification, object: nil) + } + + @objc private func keyboardWillShow(_ notification: Notification) { + guard let keyboardFrame = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect else { return } + let keyboardFrameInView = view.convert(keyboardFrame, from: nil) + let intersection = keyboardFrameInView.intersection(view.bounds) + let keyboardHeight = intersection.height + + let duration = notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? TimeInterval ?? 0.25 + + UIView.animate(withDuration: duration, delay: 0, options: .beginFromCurrentState) { + self.textView.contentInset.bottom = keyboardHeight + self.textView.verticalScrollIndicatorInsets.bottom = keyboardHeight + } + } + + @objc private func keyboardWillHide(_ notification: Notification) { + let duration = notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? TimeInterval ?? 0.25 + + UIView.animate(withDuration: duration, delay: 0, options: .beginFromCurrentState) { + self.textView.contentInset.bottom = 0 + self.textView.verticalScrollIndicatorInsets.bottom = 0 + } + } + @objc private func didTapFormatButton() { guard !trimmedText().isEmpty else { showEmptyAlert() diff --git a/Note/FormatNoteViewController.swift b/Note/FormatNoteViewController.swift index 4e111c8..7200835 100644 --- a/Note/FormatNoteViewController.swift +++ b/Note/FormatNoteViewController.swift @@ -30,6 +30,11 @@ class FormatNoteViewController: UIViewController { navigationItem.largeTitleDisplayMode = .never view.backgroundColor = .systemGroupedBackground setupUI() + setupKeyboardObservers() + } + + deinit { + NotificationCenter.default.removeObserver(self) } private func setupUI() { @@ -101,6 +106,34 @@ class FormatNoteViewController: UIViewController { textView.resignFirstResponder() } + private func setupKeyboardObservers() { + NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: UIResponder.keyboardWillShowNotification, object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: UIResponder.keyboardWillHideNotification, object: nil) + } + + @objc private func keyboardWillShow(_ notification: Notification) { + guard let keyboardFrame = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect else { return } + let keyboardFrameInView = view.convert(keyboardFrame, from: nil) + let intersection = keyboardFrameInView.intersection(view.bounds) + let keyboardHeight = intersection.height + + let duration = notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? TimeInterval ?? 0.25 + + UIView.animate(withDuration: duration, delay: 0, options: .beginFromCurrentState) { + self.textView.contentInset.bottom = keyboardHeight + self.textView.verticalScrollIndicatorInsets.bottom = keyboardHeight + } + } + + @objc private func keyboardWillHide(_ notification: Notification) { + let duration = notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? TimeInterval ?? 0.25 + + UIView.animate(withDuration: duration, delay: 0, options: .beginFromCurrentState) { + self.textView.contentInset.bottom = 0 + self.textView.verticalScrollIndicatorInsets.bottom = 0 + } + } + @objc private func didTapPasteButton() { if let string = UIPasteboard.general.string { textView.text = string