2 // IQKeyboardReturnKeyHandler+TextFieldDelegate.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 // MARK: UITextFieldDelegate
27 @available(iOSApplicationExtension, unavailable)
28 extension IQKeyboardReturnKeyHandler: UITextFieldDelegate {
30 @objc public func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
34 if let unwrapDelegate: UITextFieldDelegate = textFieldViewCachedInfo(textField)?.textFieldDelegate {
35 if unwrapDelegate.responds(to: #selector(UITextFieldDelegate.textFieldShouldBeginEditing(_:))) {
36 return unwrapDelegate.textFieldShouldBeginEditing?(textField) ?? false
44 @objc public func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
48 if let unwrapDelegate: UITextFieldDelegate = textFieldViewCachedInfo(textField)?.textFieldDelegate {
49 if unwrapDelegate.responds(to: #selector(UITextFieldDelegate.textFieldShouldEndEditing(_:))) {
50 return unwrapDelegate.textFieldShouldEndEditing?(textField) ?? false
58 @objc public func textFieldDidBeginEditing(_ textField: UITextField) {
59 updateReturnKeyTypeOnTextField(textField)
61 var aDelegate: UITextFieldDelegate? = delegate
65 if let model: IQTextFieldViewInfoModel = textFieldViewCachedInfo(textField) {
66 aDelegate = model.textFieldDelegate
70 aDelegate?.textFieldDidBeginEditing?(textField)
73 @objc public func textFieldDidEndEditing(_ textField: UITextField) {
75 var aDelegate: UITextFieldDelegate? = delegate
79 if let model: IQTextFieldViewInfoModel = textFieldViewCachedInfo(textField) {
80 aDelegate = model.textFieldDelegate
84 aDelegate?.textFieldDidEndEditing?(textField)
87 @objc public func textFieldDidEndEditing(_ textField: UITextField, reason: UITextField.DidEndEditingReason) {
89 var aDelegate: UITextFieldDelegate? = delegate
93 if let model: IQTextFieldViewInfoModel = textFieldViewCachedInfo(textField) {
94 aDelegate = model.textFieldDelegate
98 aDelegate?.textFieldDidEndEditing?(textField, reason: reason)
101 @objc public func textField(_ textField: UITextField,
102 shouldChangeCharactersIn range: NSRange,
103 replacementString string: String) -> Bool {
107 if let unwrapDelegate: UITextFieldDelegate = textFieldViewCachedInfo(textField)?.textFieldDelegate {
108 let selector: Selector = #selector(UITextFieldDelegate.textField(_:shouldChangeCharactersIn:
110 if unwrapDelegate.responds(to: selector) {
111 return unwrapDelegate.textField?(textField,
112 shouldChangeCharactersIn: range,
113 replacementString: string) ?? false
120 @objc public func textFieldShouldClear(_ textField: UITextField) -> Bool {
124 if let unwrapDelegate: UITextFieldDelegate = textFieldViewCachedInfo(textField)?.textFieldDelegate {
125 if unwrapDelegate.responds(to: #selector(UITextFieldDelegate.textFieldShouldClear(_:))) {
126 return unwrapDelegate.textFieldShouldClear?(textField) ?? false
134 @objc public func textFieldShouldReturn(_ textField: UITextField) -> Bool {
136 var isReturn: Bool = true
140 if let unwrapDelegate: UITextFieldDelegate = textFieldViewCachedInfo(textField)?.textFieldDelegate {
141 if unwrapDelegate.responds(to: #selector(UITextFieldDelegate.textFieldShouldReturn(_:))) {
142 isReturn = unwrapDelegate.textFieldShouldReturn?(textField) ?? false
148 goToNextResponderOrResign(textField)
151 return goToNextResponderOrResign(textField)