8cc195fa8e6e5da4de18245ff1bed0946ccd6ba2
[mirrors/VoiceChatSdk_iOS.git] /
1 //
2 //  IQKeyboardReturnKeyHandler+TextFieldDelegate.swift
3 //  https://github.com/hackiftekhar/IQKeyboardManager
4 //  Copyright (c) 2013-24 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
24 import UIKit
25
26 // MARK: UITextFieldDelegate
27 @available(iOSApplicationExtension, unavailable)
28 extension IQKeyboardReturnKeyHandler: UITextFieldDelegate {
29
30     @objc public func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
31
32         if delegate == nil {
33
34             if let unwrapDelegate: UITextFieldDelegate = textFieldViewCachedInfo(textField)?.textFieldDelegate {
35                 if unwrapDelegate.responds(to: #selector(UITextFieldDelegate.textFieldShouldBeginEditing(_:))) {
36                     return unwrapDelegate.textFieldShouldBeginEditing?(textField) ?? false
37                 }
38             }
39         }
40
41         return true
42     }
43
44     @objc public func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
45
46         if delegate == nil {
47
48             if let unwrapDelegate: UITextFieldDelegate = textFieldViewCachedInfo(textField)?.textFieldDelegate {
49                 if unwrapDelegate.responds(to: #selector(UITextFieldDelegate.textFieldShouldEndEditing(_:))) {
50                     return unwrapDelegate.textFieldShouldEndEditing?(textField) ?? false
51                 }
52             }
53         }
54
55         return true
56     }
57
58     @objc public func textFieldDidBeginEditing(_ textField: UITextField) {
59         updateReturnKeyTypeOnTextField(textField)
60
61         var aDelegate: UITextFieldDelegate? = delegate
62
63         if aDelegate == nil {
64
65             if let model: IQTextFieldViewInfoModel = textFieldViewCachedInfo(textField) {
66                 aDelegate = model.textFieldDelegate
67             }
68         }
69
70         aDelegate?.textFieldDidBeginEditing?(textField)
71     }
72
73     @objc public func textFieldDidEndEditing(_ textField: UITextField) {
74
75         var aDelegate: UITextFieldDelegate? = delegate
76
77         if aDelegate == nil {
78
79             if let model: IQTextFieldViewInfoModel = textFieldViewCachedInfo(textField) {
80                 aDelegate = model.textFieldDelegate
81             }
82         }
83
84         aDelegate?.textFieldDidEndEditing?(textField)
85     }
86
87     @objc public func textFieldDidEndEditing(_ textField: UITextField, reason: UITextField.DidEndEditingReason) {
88
89         var aDelegate: UITextFieldDelegate? = delegate
90
91         if aDelegate == nil {
92
93             if let model: IQTextFieldViewInfoModel = textFieldViewCachedInfo(textField) {
94                 aDelegate = model.textFieldDelegate
95             }
96         }
97
98         aDelegate?.textFieldDidEndEditing?(textField, reason: reason)
99     }
100
101     @objc public func textField(_ textField: UITextField,
102                                 shouldChangeCharactersIn range: NSRange,
103                                 replacementString string: String) -> Bool {
104
105         if delegate == nil {
106
107             if let unwrapDelegate: UITextFieldDelegate = textFieldViewCachedInfo(textField)?.textFieldDelegate {
108                 let selector: Selector = #selector(UITextFieldDelegate.textField(_:shouldChangeCharactersIn:
109                                                                                     replacementString:))
110                 if unwrapDelegate.responds(to: selector) {
111                     return unwrapDelegate.textField?(textField,
112                                                      shouldChangeCharactersIn: range,
113                                                      replacementString: string) ?? false
114                 }
115             }
116         }
117         return true
118     }
119
120     @objc public func textFieldShouldClear(_ textField: UITextField) -> Bool {
121
122         if delegate == nil {
123
124             if let unwrapDelegate: UITextFieldDelegate = textFieldViewCachedInfo(textField)?.textFieldDelegate {
125                 if unwrapDelegate.responds(to: #selector(UITextFieldDelegate.textFieldShouldClear(_:))) {
126                     return unwrapDelegate.textFieldShouldClear?(textField) ?? false
127                 }
128             }
129         }
130
131         return true
132     }
133
134     @objc public func textFieldShouldReturn(_ textField: UITextField) -> Bool {
135
136         var isReturn: Bool = true
137
138         if delegate == nil {
139
140             if let unwrapDelegate: UITextFieldDelegate = textFieldViewCachedInfo(textField)?.textFieldDelegate {
141                 if unwrapDelegate.responds(to: #selector(UITextFieldDelegate.textFieldShouldReturn(_:))) {
142                     isReturn = unwrapDelegate.textFieldShouldReturn?(textField) ?? false
143                 }
144             }
145         }
146
147         if isReturn {
148             goToNextResponderOrResign(textField)
149             return true
150         } else {
151             return goToNextResponderOrResign(textField)
152         }
153     }
154 }