Skip to content

Commit 44307c7

Browse files
committed
Added text field disable action
1 parent c91754e commit 44307c7

File tree

6 files changed

+102
-21
lines changed

6 files changed

+102
-21
lines changed

Example/FloatingLabelTextFieldSwiftUI/ContentView.swift

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,26 +46,28 @@ struct ContentView: View {
4646
}) {
4747

4848
}
49+
.addDisableEditingAction([.all])
4950
.isShowError(true)
5051
.addValidations([.init(condition: firstName.isValid(.alphabet), errorMessage: "Invalid Name"),
5152
.init(condition: firstName.count >= 2, errorMessage: "Minimum two character long")
5253
])
53-
.isRequiredField(true, with: "Name field is required")
54-
.floatingStyle(ThemeTextFieldStyle())
55-
.modifier(ThemeTextField())
54+
.isRequiredField(true, with: "Name field is required")
55+
.floatingStyle(ThemeTextFieldStyle())
56+
.modifier(ThemeTextField())
5657

5758

5859
FloatingLabelTextField($lastName, placeholder: "Last Name", editingChanged: { (isChanged) in
5960

6061
}) {
6162

6263
}
64+
.addDisableEditingAction([.paste])
6365
.isShowError(true)
6466
.addValidations([.init(condition: lastName.isValid(.alphabet), errorMessage: "Invalid Name"),
6567
.init(condition: lastName.count >= 2, errorMessage: "Minimum two character long")
6668
])
67-
.floatingStyle(ThemeTextFieldStyle2())
68-
.modifier(ThemeTextField())
69+
.floatingStyle(ThemeTextFieldStyle2())
70+
.modifier(ThemeTextField())
6971
}
7072

7173
FloatingLabelTextField($birthDate, placeholder: "Birth Date", editingChanged: { (isChanged) in
@@ -85,6 +87,7 @@ struct ContentView: View {
8587
}) {
8688

8789
}
90+
.addDisableEditingAction([.paste])
8891
.keyboardType(.phonePad)
8992
.modifier(ThemeTextField())
9093
FloatingLabelTextField($email, validtionChecker: $isValidEmail, placeholder: "Email", editingChanged: { (isChanged) in
@@ -94,15 +97,16 @@ struct ContentView: View {
9497
}
9598
.addValidations([.init(condition: email.isValid(.email), errorMessage: "Invalid Email")
9699
])
97-
.isShowError(true)
98-
.keyboardType(.emailAddress)
99-
.modifier(ThemeTextField())
100+
.isShowError(true)
101+
.keyboardType(.emailAddress)
102+
.modifier(ThemeTextField())
100103

101104
FloatingLabelTextField($password, placeholder: "Password", editingChanged: { (isChanged) in
102105

103106
}) {
104107

105108
}
109+
.addDisableEditingAction([.all])
106110
.isShowError(true)
107111
.isRequiredField(true, with: "Password field is required")
108112
.rightView({
@@ -115,8 +119,8 @@ struct ContentView: View {
115119
Image(self.isPasswordShow ? "eye_close" : "eye_show")
116120
}
117121
})
118-
.isSecureTextEntry(!self.isPasswordShow)
119-
.modifier(ThemeTextField())
122+
.isSecureTextEntry(!self.isPasswordShow)
123+
.modifier(ThemeTextField())
120124
// SecureField("", text: $password)
121125
// Text(password)
122126
Button(action: {
@@ -135,7 +139,7 @@ struct ContentView: View {
135139
.buttonStyle(CreateButtonStyle())
136140
Spacer()
137141
}
138-
142+
139143
.padding()
140144

141145
}

Example/Pods/Pods.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ struct ThemeTextFieldStyle: FloatingLabelTextFieldStyle {
102102
.placeholderColor(.gray) // Sets the placeholder color.
103103
.placeholderFont(.system(size: 15)) // Sets the placeholder font.
104104
.errorColor(.red) /// Sets the error color.
105+
.addDisableEditingAction([.paste]) /// Disable text field editing action. Like cut, copy, past, all etc.
105106
}
106107
}
107108
```
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
//
2+
// UITextField+Extension.swift
3+
// FloatingLabelTextFieldSwiftUI
4+
//
5+
// Created by KISHAN_RAJA on 18/11/20.
6+
// Copyright © 2020 KISHAN_RAJA. All rights reserved.
7+
//
8+
9+
import UIKit
10+
11+
/// All the text field editing actions with selector.
12+
public enum TextFieldEditActions {
13+
case all
14+
case copy, paste, select, selectAll, delete, cut
15+
case decreaseSize, increaseSize
16+
case toggleItalics, toggleBoldface, toggleUnderline
17+
case makeTextWritingDirectionLeftToRight, makeTextWritingDirectionRightToLeft
18+
19+
var selector: Selector {
20+
switch self {
21+
case .all:
22+
return #selector(UIResponderStandardEditActions.`self`)
23+
case .copy:
24+
return #selector(UIResponderStandardEditActions.copy)
25+
case .paste:
26+
return #selector(UIResponderStandardEditActions.paste)
27+
case .select:
28+
return #selector(UIResponderStandardEditActions.select)
29+
case .selectAll:
30+
return #selector(UIResponderStandardEditActions.selectAll)
31+
case .delete:
32+
return #selector(UIResponderStandardEditActions.delete)
33+
case .cut:
34+
return #selector(UIResponderStandardEditActions.cut)
35+
case .decreaseSize:
36+
return #selector(UIResponderStandardEditActions.decreaseSize)
37+
case .increaseSize:
38+
return #selector(UIResponderStandardEditActions.increaseSize)
39+
case .toggleItalics:
40+
return #selector(UIResponderStandardEditActions.toggleItalics)
41+
case .toggleBoldface:
42+
return #selector(UIResponderStandardEditActions.toggleBoldface)
43+
case .toggleUnderline:
44+
return #selector(UIResponderStandardEditActions.toggleUnderline)
45+
case .makeTextWritingDirectionLeftToRight:
46+
return #selector(UIResponderStandardEditActions.makeTextWritingDirectionLeftToRight)
47+
case .makeTextWritingDirectionRightToLeft:
48+
return #selector(UIResponderStandardEditActions.makeTextWritingDirectionRightToLeft)
49+
}
50+
}
51+
}
52+
53+
/// To store the editing action which is not allowed the user to show..
54+
public var arrTextFieldEditActions: [TextFieldEditActions] = []
55+
56+
extension UITextField {
57+
open override func target(forAction action: Selector, withSender sender: Any?) -> Any? {
58+
59+
if arrTextFieldEditActions.contains(where: {$0 == .all}) || arrTextFieldEditActions.contains(where: {$0.selector == action}) {
60+
return nil
61+
}
62+
63+
return super.target(forAction: action, withSender: sender)
64+
}
65+
}

Sources/FloatingLabelTextFieldSwiftUI/FloatingLabelTextField.swift

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ public struct FloatingLabelTextField: View {
7676
}
7777
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
7878
if let currentResponder = UIResponder.currentFirstResponder, let currentTextField = currentResponder.globalView as? UITextField{
79+
arrTextFieldEditActions = notifier.arrTextFieldEditActions
7980
self.isSelected = self.notifier.isSecureTextEntry
8081
currentTextField.addAction(for: .editingDidEnd) {
8182
self.isSelected = false
@@ -95,15 +96,15 @@ public struct FloatingLabelTextField: View {
9596
self.validtionChecker = self.currentError.condition
9697
self.editingChanged(isChanged)
9798
self.isShowError = self.notifier.isRequiredField
98-
99+
arrTextFieldEditActions = notifier.arrTextFieldEditActions
99100
}, onCommit: {
100101
self.isShowError = self.notifier.isRequiredField
101102
self.validtionChecker = self.currentError.condition
102103
self.commit()
103104
})
104-
.multilineTextAlignment(notifier.textAlignment)
105-
.font(notifier.font)
106-
.foregroundColor((self.currentError.condition || !notifier.isShowError) ? (isSelected ? notifier.selectedTextColor : notifier.textColor) : notifier.errorColor)
105+
.multilineTextAlignment(notifier.textAlignment)
106+
.font(notifier.font)
107+
.foregroundColor((self.currentError.condition || !notifier.isShowError) ? (isSelected ? notifier.selectedTextColor : notifier.textColor) : notifier.errorColor)
107108
}
108109
}
109110
}
@@ -332,9 +333,12 @@ extension FloatingLabelTextField {
332333
}
333334
}
334335

335-
//MARK: Preview
336-
//struct FloatingLabelTextField_Previews: PreviewProvider {
337-
// static var previews: some View {
338-
// FloatingLabelTextField()
339-
// }
340-
//}
336+
//MARK: Text Field Editing Funcation
337+
@available(iOS 13.0, *)
338+
extension FloatingLabelTextField {
339+
/// Disable text field editing action. Like cut, copy, past, all etc.
340+
public func addDisableEditingAction(_ actions: [TextFieldEditActions]) -> Self {
341+
notifier.arrTextFieldEditActions = actions
342+
return self
343+
}
344+
}

Sources/FloatingLabelTextFieldSwiftUI/ObservableObject/ObservableObject.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,7 @@ class FloatingLabelTextFieldNotifier: ObservableObject {
4848
@Published var arrValidator: [TextFieldValidator] = []
4949
@Published var isRequiredField: Bool = false
5050
@Published var requiredFieldMessage: String = ""
51+
52+
//MARK: Action Editing Properties
53+
@Published var arrTextFieldEditActions: [TextFieldEditActions] = []
5154
}

0 commit comments

Comments
 (0)