2 // IQTextFieldViewListener.swift
3 // https://github.com/hackiftekhar/IQKeyboardManager
4 // Copyright (c) 2013-24 Iftekhar Qurashi.
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:
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
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
26 @available(iOSApplicationExtension, unavailable)
28 public class IQTextFieldViewListener {
30 private var textFieldViewObservers: [AnyHashable: TextFieldViewCompletion] = [:]
33 private(set) var lastTextFieldViewInfo: IQTextFieldViewInfo?
36 private(set) var textFieldViewInfo: IQTextFieldViewInfo?
38 public var textFieldView: UIView? {
39 return textFieldViewInfo?.textFieldView
43 // Registering for keyboard notification.
44 NotificationCenter.default.addObserver(self, selector: #selector(self.didBeginEditing(_:)),
45 name: UITextField.textDidBeginEditingNotification, object: nil)
46 NotificationCenter.default.addObserver(self, selector: #selector(self.didEndEditing(_:)),
47 name: UITextField.textDidEndEditingNotification, object: nil)
49 NotificationCenter.default.addObserver(self, selector: #selector(self.didBeginEditing(_:)),
50 name: UITextView.textDidBeginEditingNotification, object: nil)
51 NotificationCenter.default.addObserver(self, selector: #selector(self.didEndEditing(_:)),
52 name: UITextView.textDidEndEditingNotification, object: nil)
55 @objc private func didBeginEditing(_ notification: Notification) {
56 guard let info: IQTextFieldViewInfo = IQTextFieldViewInfo(notification: notification,
57 name: .beginEditing) else {
63 if #available(iOS 16.0, *),
64 let lastTextFieldViewInfo = lastTextFieldViewInfo,
65 let textView: UITextView = lastTextFieldViewInfo.textFieldView as? UITextView,
66 textView.findInteraction?.isFindNavigatorVisible == true {
67 // // This means the this didBeginEditing call comes due to find interaction
68 textFieldViewInfo = lastTextFieldViewInfo
69 sendEvent(info: lastTextFieldViewInfo)
70 } else if textFieldViewInfo != info {
71 textFieldViewInfo = info
72 lastTextFieldViewInfo = nil
75 lastTextFieldViewInfo = nil
78 if textFieldViewInfo != info {
79 textFieldViewInfo = info
85 @objc private func didEndEditing(_ notification: Notification) {
86 guard let info: IQTextFieldViewInfo = IQTextFieldViewInfo(notification: notification, name: .endEditing) else {
90 if textFieldViewInfo != info {
92 if #available(iOS 16.0, *),
93 let textView: UITextView = info.textFieldView as? UITextView,
94 textView.isFindInteractionEnabled {
95 lastTextFieldViewInfo = textFieldViewInfo
97 lastTextFieldViewInfo = nil
100 textFieldViewInfo = info
101 sendEvent(info: info)
102 textFieldViewInfo = nil
107 @available(iOSApplicationExtension, unavailable)
108 public extension IQTextFieldViewListener {
110 typealias TextFieldViewCompletion = (_ info: IQTextFieldViewInfo) -> Void
112 func registerTextFieldViewChange(identifier: AnyHashable, changeHandler: @escaping TextFieldViewCompletion) {
113 textFieldViewObservers[identifier] = changeHandler
116 func unregisterSizeChange(identifier: AnyHashable) {
117 textFieldViewObservers[identifier] = nil
120 private func sendEvent(info: IQTextFieldViewInfo) {
122 for block in textFieldViewObservers.values {