Compare commits
2 Commits
f1a380d105
...
255f171aa8
| Author | SHA1 | Date | |
|---|---|---|---|
| 255f171aa8 | |||
| 70870e1ec2 |
@@ -29,6 +29,41 @@ class ClipEditViewController: UIViewController {
|
|||||||
|
|
||||||
setupNavigationBar()
|
setupNavigationBar()
|
||||||
setupTextView()
|
setupTextView()
|
||||||
|
setupKeyboardObservers()
|
||||||
|
}
|
||||||
|
|
||||||
|
deinit {
|
||||||
|
NotificationCenter.default.removeObserver(self)
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
self.view.layoutIfNeeded()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@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
|
||||||
|
self.view.layoutIfNeeded()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override func viewDidAppear(_ animated: Bool) {
|
override func viewDidAppear(_ animated: Bool) {
|
||||||
@@ -59,6 +94,7 @@ class ClipEditViewController: UIViewController {
|
|||||||
textView.font = UIFont.preferredFont(forTextStyle: .body)
|
textView.font = UIFont.preferredFont(forTextStyle: .body)
|
||||||
textView.textContainerInset = UIEdgeInsets(top: 12, left: 12, bottom: 12, right: 12)
|
textView.textContainerInset = UIEdgeInsets(top: 12, left: 12, bottom: 12, right: 12)
|
||||||
textView.text = clip.text
|
textView.text = clip.text
|
||||||
|
textView.inputAccessoryView = createInputAccessoryView()
|
||||||
view.addSubview(textView)
|
view.addSubview(textView)
|
||||||
|
|
||||||
let safeArea = view.safeAreaLayoutGuide
|
let safeArea = view.safeAreaLayoutGuide
|
||||||
@@ -70,6 +106,22 @@ class ClipEditViewController: UIViewController {
|
|||||||
])
|
])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private func createInputAccessoryView() -> UIToolbar {
|
||||||
|
let toolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: view.bounds.width, height: 44))
|
||||||
|
toolbar.barStyle = .default
|
||||||
|
toolbar.isTranslucent = true
|
||||||
|
|
||||||
|
let flexSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
|
||||||
|
let doneButton = UIBarButtonItem(title: "完成", style: .done, target: self, action: #selector(didTapKeyboardDoneButton))
|
||||||
|
toolbar.items = [flexSpace, doneButton]
|
||||||
|
toolbar.sizeToFit()
|
||||||
|
return toolbar
|
||||||
|
}
|
||||||
|
|
||||||
|
@objc private func didTapKeyboardDoneButton() {
|
||||||
|
textView.resignFirstResponder()
|
||||||
|
}
|
||||||
|
|
||||||
@objc private func didTapSaveButton() {
|
@objc private func didTapSaveButton() {
|
||||||
let updatedText = textView.text ?? ""
|
let updatedText = textView.text ?? ""
|
||||||
guard !updatedText.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else {
|
guard !updatedText.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else {
|
||||||
|
|||||||
Reference in New Issue
Block a user