Skip to content

Commit

Permalink
add: UITextFieldDelegate & UITextViewDelegate call forwarding
Browse files Browse the repository at this point in the history
  • Loading branch information
taflanidi committed Jun 1, 2023
1 parent a09ae72 commit d499c40
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 4 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,11 @@
# 𝌡Changelog

## 7.2.7

**⤵️ Added:**

* `MaskedTextInputListener` now provides call forwarding to its corresponding `textFieldDelegate` and `textViewDelegate`

## 7.2.6

**🔄 Modified:**
Expand Down
2 changes: 1 addition & 1 deletion InputMask.podspec
@@ -1,6 +1,6 @@
Pod::Spec.new do |spec|
spec.name = "InputMask"
spec.version = "7.2.6"
spec.version = "7.2.7"
spec.summary = "InputMask"
spec.description = "User input masking library."
spec.homepage = "https://github.com/RedMadRobot/input-mask-ios"
Expand Down
2 changes: 1 addition & 1 deletion README.md
@@ -1,6 +1,6 @@
<img src="Documentation/Assets/logo.png" alt="Input Mask" />

[![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2FRedMadRobot%2Finput-mask-ios%2Fbadge%3Ftype%3Dswift-versions&style=for-the-badge)](https://swiftpackageindex.com/RedMadRobot/input-mask-ios) [![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2FRedMadRobot%2Finput-mask-ios%2Fbadge%3Ftype%3Dplatforms&style=for-the-badge)](https://swiftpackageindex.com/RedMadRobot/input-mask-ios) [![Pod Version Badge](https://img.shields.io/badge/POD-v7.2.6-blue?logo=cocoapods&style=for-the-badge)](https://cocoapods.org/pods/InputMask) [![Awesome](https://img.shields.io/badge/-mentioned_in_awesome_iOS-CCA6C4.svg?colorA=CCA6C4&colorB=261120&logoWidth=20&logo=data%3Aimage%2Fsvg%2Bxml%3Bbase64%2CPHN2ZyB3aWR0aD0iMjAiIGhlaWdodD0iMTAiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI%2BICAgIDxwYXRoIGZpbGw9IiMyNjExMjAiIGQ9Ik0xOS4xNCA0LjVMMTQuMjMgMGwtLjY5Ljc1IDQuMDkgMy43NUgxLjUxTDUuNi43NSA0LjkxIDAgMCA0LjV2Mi45N0MwIDguODEgMS4yOSA5LjkgMi44OCA5LjloMy4wM2MxLjU5IDAgMi44OC0xLjA5IDIuODgtMi40M1Y1LjUyaDEuNTd2MS45NWMwIDEuMzQgMS4yOSAyLjQzIDIuODggMi40M2gzLjAzYzEuNTkgMCAyLjg4LTEuMDkgMi44OC0yLjQzbC0uMDEtMi45N3oiLz48L3N2Zz4%3D&style=for-the-badge)](https://github.com/vsouza/awesome-ios) [![Actions](https://img.shields.io/github/actions/workflow/status/RedMadRobot/input-mask-ios/swift.yml?style=for-the-badge)](https://github.com/RedMadRobot/input-mask-ios/actions/workflows/swift.yml) [![Android](https://img.shields.io/badge/-android_version-red?color=teal&logo=android&style=for-the-badge)](https://github.com/RedMadRobot/input-mask-android) [![Telegram](https://img.shields.io/badge/-telegram_author-red?color=blue&logo=telegram&style=for-the-badge)](https://t.me/jeorge_taflanidi) [![license](https://img.shields.io/github/license/mashape/apistatus.svg?style=for-the-badge)](#license)
[![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2FRedMadRobot%2Finput-mask-ios%2Fbadge%3Ftype%3Dswift-versions&style=for-the-badge)](https://swiftpackageindex.com/RedMadRobot/input-mask-ios) [![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2FRedMadRobot%2Finput-mask-ios%2Fbadge%3Ftype%3Dplatforms&style=for-the-badge)](https://swiftpackageindex.com/RedMadRobot/input-mask-ios) [![Pod Version Badge](https://img.shields.io/badge/POD-v7.2.7-blue?logo=cocoapods&style=for-the-badge)](https://cocoapods.org/pods/InputMask) [![Awesome](https://img.shields.io/badge/-mentioned_in_awesome_iOS-CCA6C4.svg?colorA=CCA6C4&colorB=261120&logoWidth=20&logo=data%3Aimage%2Fsvg%2Bxml%3Bbase64%2CPHN2ZyB3aWR0aD0iMjAiIGhlaWdodD0iMTAiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI%2BICAgIDxwYXRoIGZpbGw9IiMyNjExMjAiIGQ9Ik0xOS4xNCA0LjVMMTQuMjMgMGwtLjY5Ljc1IDQuMDkgMy43NUgxLjUxTDUuNi43NSA0LjkxIDAgMCA0LjV2Mi45N0MwIDguODEgMS4yOSA5LjkgMi44OCA5LjloMy4wM2MxLjU5IDAgMi44OC0xLjA5IDIuODgtMi40M1Y1LjUyaDEuNTd2MS45NWMwIDEuMzQgMS4yOSAyLjQzIDIuODggMi40M2gzLjAzYzEuNTkgMCAyLjg4LTEuMDkgMi44OC0yLjQzbC0uMDEtMi45N3oiLz48L3N2Zz4%3D&style=for-the-badge)](https://github.com/vsouza/awesome-ios) [![Actions](https://img.shields.io/github/actions/workflow/status/RedMadRobot/input-mask-ios/swift.yml?style=for-the-badge)](https://github.com/RedMadRobot/input-mask-ios/actions/workflows/swift.yml) [![Android](https://img.shields.io/badge/-android_version-red?color=teal&logo=android&style=for-the-badge)](https://github.com/RedMadRobot/input-mask-android) [![Telegram](https://img.shields.io/badge/-telegram_author-red?color=blue&logo=telegram&style=for-the-badge)](https://t.me/jeorge_taflanidi) [![license](https://img.shields.io/github/license/mashape/apistatus.svg?style=for-the-badge)](#license)

Input masks restrict data input and allow you to guide users to enter correct values.
Check out our [wiki](https://github.com/RedMadRobot/input-mask-ios/wiki) for quick start and further reading.
Expand Down
4 changes: 2 additions & 2 deletions Source/InputMask/InputMask/Classes/View/MaskedTextField.swift
Expand Up @@ -332,9 +332,9 @@ public struct MaskedTextField: UIViewRepresentable {
)
}

public func textFieldShouldReturn(_ textField: UITextField) -> Bool {
public override func textFieldShouldReturn(_ textField: UITextField) -> Bool {
onSubmit?(textField)
return true
return super.textFieldShouldReturn(textField)
}

public override func textFieldDidBeginEditing(_ textField: UITextField) {
Expand Down
Expand Up @@ -26,6 +26,9 @@ open class MaskedTextInputListener: NSObject {

open weak var listener: OnMaskedTextChangedListener?
open var onMaskedTextChangedCallback: ((_ textInput: UITextInput, _ value: String, _ complete: Bool, _ tailPlaceholder: String) -> ())?

open weak var textFieldDelegate: UITextFieldDelegate?
open weak var textViewDelegate: UITextViewDelegate?

@IBInspectable open var primaryMaskFormat: String
@IBInspectable open var autocomplete: Bool
Expand Down Expand Up @@ -355,6 +358,7 @@ open class MaskedTextInputListener: NSObject {
extension MaskedTextInputListener: UITextFieldDelegate {

open func textFieldDidBeginEditing(_ textField: UITextField) {
textFieldDelegate?.textFieldDidBeginEditing?(textField)
if autocompleteOnFocus && (textField.text ?? "").isEmpty {
let result: Mask.Result = put(text: "", into: textField, autocomplete: true)
notifyOnMaskedTextChangedListeners(forTextInput: textField, result: result)
Expand All @@ -366,6 +370,11 @@ extension MaskedTextInputListener: UITextFieldDelegate {
shouldChangeCharactersIn range: NSRange,
replacementString string: String
) -> Bool {
// NOTE: lib logic depends on controlling the returned value, so no full control forwarding is allowed here
if textFieldDelegate?.textField?(textField, shouldChangeCharactersIn: range, replacementString: string) == false {
return false
}

switch textInput(textField, isChangingCharactersIn: range, replacementString: string) {
case .fallback:
return true
Expand All @@ -376,10 +385,46 @@ extension MaskedTextInputListener: UITextFieldDelegate {
}

open func textFieldShouldClear(_ textField: UITextField) -> Bool {
if textFieldDelegate?.textFieldShouldClear?(textField) == false {
return false
}

let result: Mask.Result = put(text: "", into: textField, autocomplete: false)
notifyOnMaskedTextChangedListeners(forTextInput: textField, result: result)
return true
}

// MARK: - Call forwarding

public func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
return textFieldDelegate?.textFieldShouldBeginEditing?(textField) ?? true
}

public func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
return textFieldDelegate?.textFieldShouldEndEditing?(textField) ?? true
}

public func textFieldDidEndEditing(_ textField: UITextField) {
textFieldDelegate?.textFieldDidEndEditing?(textField)
}

public func textFieldDidEndEditing(_ textField: UITextField, reason: UITextField.DidEndEditingReason) {
textFieldDelegate?.textFieldDidEndEditing?(textField, reason: reason)
}

@available(tvOS 13.0, *)
public func textFieldDidChangeSelection(_ textField: UITextField) {
textFieldDelegate?.textFieldDidChangeSelection?(textField)
}

public func textFieldShouldReturn(_ textField: UITextField) -> Bool {
return textFieldDelegate?.textFieldShouldReturn?(textField) ?? true
}

@available(tvOS 16.0, *)
public func textField(_ textField: UITextField, editMenuForCharactersIn range: NSRange, suggestedActions: [UIMenuElement]) -> UIMenu? {
return textFieldDelegate?.textField?(textField, editMenuForCharactersIn: range, suggestedActions: suggestedActions)
}

}

Expand All @@ -388,6 +433,7 @@ extension MaskedTextInputListener: UITextFieldDelegate {
extension MaskedTextInputListener: UITextViewDelegate {

open func textViewDidBeginEditing(_ textView: UITextView) {
textViewDelegate?.textViewDidBeginEditing?(textView)
if autocompleteOnFocus && textView.text.isEmpty {
let result: Mask.Result = put(text: "", into: textView, autocomplete: true)
notifyOnMaskedTextChangedListeners(forTextInput: textView, result: result)
Expand All @@ -399,6 +445,11 @@ extension MaskedTextInputListener: UITextViewDelegate {
shouldChangeTextIn range: NSRange,
replacementText text: String
) -> Bool {
// NOTE: lib logic depends on controlling the returned value, so no full control forwarding is allowed here
if textViewDelegate?.textView?(textView, shouldChangeTextIn: range, replacementText: text) == false {
return false
}

switch textInput(textView, isChangingCharactersIn: range, replacementString: text) {
case .fallback:
return true
Expand All @@ -407,6 +458,51 @@ extension MaskedTextInputListener: UITextViewDelegate {
return false
}
}

// MARK: - Call forwarding

public func textViewShouldBeginEditing(_ textView: UITextView) -> Bool {
return textViewDelegate?.textViewShouldBeginEditing?(textView) ?? true
}

public func textViewShouldEndEditing(_ textView: UITextView) -> Bool {
return textViewDelegate?.textViewShouldEndEditing?(textView) ?? true
}

public func textViewDidEndEditing(_ textView: UITextView) {
textViewDelegate?.textViewDidEndEditing?(textView)
}

public func textViewDidChange(_ textView: UITextView) {
textViewDelegate?.textViewDidChange?(textView)
}

public func textViewDidChangeSelection(_ textView: UITextView) {
textViewDelegate?.textViewDidChangeSelection?(textView)
}

public func textView(
_ textView: UITextView,
shouldInteractWith URL: URL,
in characterRange: NSRange,
interaction: UITextItemInteraction
) -> Bool {
return textViewDelegate?.textView?(textView, shouldInteractWith: URL, in: characterRange, interaction: interaction) ?? true
}

public func textView(
_ textView: UITextView,
shouldInteractWith textAttachment: NSTextAttachment,
in characterRange: NSRange,
interaction: UITextItemInteraction
) -> Bool {
return textViewDelegate?.textView?(textView, shouldInteractWith: textAttachment, in: characterRange, interaction: interaction) ?? true
}

@available(tvOS 16.0, *)
public func textView(_ textView: UITextView, editMenuForTextIn range: NSRange, suggestedActions: [UIMenuElement]) -> UIMenu? {
return textViewDelegate?.textView?(textView, editMenuForTextIn: range, suggestedActions: suggestedActions)
}

}

Expand Down

0 comments on commit d499c40

Please sign in to comment.