IQKeyboardManager+Debug.swift 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //
  2. // IQKeyboardManager+Debug.swift
  3. // https://github.com/hackiftekhar/IQKeyboardManager
  4. // Copyright (c) 2013-20 Iftekhar Qurashi.
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. // THE SOFTWARE.
  23. // import Foundation - UIKit contains Foundation
  24. import UIKit
  25. // MARK: Debugging & Developer options
  26. @available(iOSApplicationExtension, unavailable)
  27. public extension IQKeyboardManager {
  28. private struct AssociatedKeys {
  29. static var enableDebugging = "enableDebugging"
  30. }
  31. @objc var enableDebugging: Bool {
  32. get {
  33. return objc_getAssociatedObject(self, &AssociatedKeys.enableDebugging) as? Bool ?? false
  34. }
  35. set(newValue) {
  36. objc_setAssociatedObject(self, &AssociatedKeys.enableDebugging, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
  37. }
  38. }
  39. /**
  40. @warning Use below methods to completely enable/disable notifications registered by library internally.
  41. Please keep in mind that library is totally dependent on NSNotification of UITextField, UITextField, Keyboard etc.
  42. If you do unregisterAllNotifications then library will not work at all. You should only use below methods if you want to completedly disable all library functions.
  43. You should use below methods at your own risk.
  44. */
  45. @objc func registerAllNotifications() {
  46. // Registering for keyboard notification.
  47. NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
  48. NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardDidShow(_:)), name: UIResponder.keyboardDidShowNotification, object: nil)
  49. NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide(_:)), name: UIResponder.keyboardWillHideNotification, object: nil)
  50. NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardDidHide(_:)), name: UIResponder.keyboardDidHideNotification, object: nil)
  51. // Registering for UITextField notification.
  52. registerTextFieldViewClass(UITextField.self, didBeginEditingNotificationName: UITextField.textDidBeginEditingNotification.rawValue, didEndEditingNotificationName: UITextField.textDidEndEditingNotification.rawValue)
  53. // Registering for UITextView notification.
  54. registerTextFieldViewClass(UITextView.self, didBeginEditingNotificationName: UITextView.textDidBeginEditingNotification.rawValue, didEndEditingNotificationName: UITextView.textDidEndEditingNotification.rawValue)
  55. // Registering for orientation changes notification
  56. NotificationCenter.default.addObserver(self, selector: #selector(self.willChangeStatusBarOrientation(_:)), name: UIApplication.willChangeStatusBarOrientationNotification, object: UIApplication.shared)
  57. }
  58. @objc func unregisterAllNotifications() {
  59. // Unregistering for keyboard notification.
  60. NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillShowNotification, object: nil)
  61. NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardDidShowNotification, object: nil)
  62. NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillHideNotification, object: nil)
  63. NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardDidHideNotification, object: nil)
  64. // Unregistering for UITextField notification.
  65. unregisterTextFieldViewClass(UITextField.self, didBeginEditingNotificationName: UITextField.textDidBeginEditingNotification.rawValue, didEndEditingNotificationName: UITextField.textDidEndEditingNotification.rawValue)
  66. // Unregistering for UITextView notification.
  67. unregisterTextFieldViewClass(UITextView.self, didBeginEditingNotificationName: UITextView.textDidBeginEditingNotification.rawValue, didEndEditingNotificationName: UITextView.textDidEndEditingNotification.rawValue)
  68. // Unregistering for orientation changes notification
  69. NotificationCenter.default.removeObserver(self, name: UIApplication.willChangeStatusBarOrientationNotification, object: UIApplication.shared)
  70. }
  71. struct Static {
  72. static var indentation = 0
  73. }
  74. internal func showLog(_ logString: String, indentation: Int = 0) {
  75. guard enableDebugging else {
  76. return
  77. }
  78. if indentation < 0 {
  79. Static.indentation = max(0, Static.indentation + indentation)
  80. }
  81. var preLog = "IQKeyboardManager"
  82. for _ in 0 ... Static.indentation {
  83. preLog += "|\t"
  84. }
  85. print(preLog + logString)
  86. if indentation > 0 {
  87. Static.indentation += indentation
  88. }
  89. }
  90. }