From fa9af8155ac1f383048a62f7fea549eb5983099e Mon Sep 17 00:00:00 2001 From: dankito Date: Sat, 8 Aug 2020 18:21:36 +0200 Subject: [PATCH] Implemented LabelledUIKitTextField that displays a label on the left and a text field on the right --- .../Base.lproj/Localizable.strings | 4 +- .../de.lproj/Localizable.strings | 4 +- .../ui/LabelledUIKitTextField.swift | 62 +++++++++++++++++++ .../BankingiOSApp/ui/UIKitTextField.swift | 27 +++++++- .../ui/views/AddAccountDialog.swift | 4 +- 5 files changed, 92 insertions(+), 9 deletions(-) create mode 100644 ui/BankingiOSApp/BankingiOSApp/ui/LabelledUIKitTextField.swift diff --git a/ui/BankingiOSApp/BankingiOSApp/Base.lproj/Localizable.strings b/ui/BankingiOSApp/BankingiOSApp/Base.lproj/Localizable.strings index d24c50c7..4494a826 100644 --- a/ui/BankingiOSApp/BankingiOSApp/Base.lproj/Localizable.strings +++ b/ui/BankingiOSApp/BankingiOSApp/Base.lproj/Localizable.strings @@ -29,8 +29,8 @@ "Select your bank ..." = "Select your bank ..."; "Bank code" = "Bank code"; -"Customer ID" = "Online banking login name"; -"Password" = "Online banking password"; +"Online banking login name" = "Login name"; +"Online banking login password" = "Password"; "Could not add account" = "Could not add account"; "Error message from your bank %@" = "Error message from your bank:\n\n%@"; diff --git a/ui/BankingiOSApp/BankingiOSApp/de.lproj/Localizable.strings b/ui/BankingiOSApp/BankingiOSApp/de.lproj/Localizable.strings index 4d165264..2601c4ec 100644 --- a/ui/BankingiOSApp/BankingiOSApp/de.lproj/Localizable.strings +++ b/ui/BankingiOSApp/BankingiOSApp/de.lproj/Localizable.strings @@ -29,8 +29,8 @@ "Select your bank ..." = "Bank auswählen ..."; "Bank code" = "Bankleitzahl"; -"Customer ID" = "Online-Banking Login Name"; -"Password" = "Online-Banking passwort"; +"Online banking login name" = "Login Name"; +"Online banking login password" = "Passwort"; "Could not add account" = "Konto konnte nicht hinzugefügt werden."; "Error message from your bank %@" = "Fehlermeldung Ihrer Bank:\n\n%@"; diff --git a/ui/BankingiOSApp/BankingiOSApp/ui/LabelledUIKitTextField.swift b/ui/BankingiOSApp/BankingiOSApp/ui/LabelledUIKitTextField.swift new file mode 100644 index 00000000..819ac372 --- /dev/null +++ b/ui/BankingiOSApp/BankingiOSApp/ui/LabelledUIKitTextField.swift @@ -0,0 +1,62 @@ +import SwiftUI + + +struct LabelledUIKitTextField: View { + + let label: LocalizedStringKey + + @Binding var text: String + + var placeholder: String = "" + + var keyboardType: UIKeyboardType = .default + var isPasswordField: Bool = false + + var focusOnStart = false + var focusNextTextFieldOnReturnKeyPress = false + + var isUserInputEnabled: Bool = true + + var actionOnReturnKeyPress: (() -> Bool)? = nil + + var textChanged: ((String) -> Void)? = nil + + + var body: some View { + HStack { + Text(label) + + Spacer() + + UIKitTextField(placeholder, text: $text, keyboardType: keyboardType, isPasswordField: isPasswordField, + focusOnStart: focusOnStart, focusNextTextFieldOnReturnKeyPress: focusNextTextFieldOnReturnKeyPress, + textAlignment: .right, isUserInputEnabled: isUserInputEnabled, + actionOnReturnKeyPress: actionOnReturnKeyPress, textChanged: textChanged) + } + } + +} + + +struct LabelledUIKitTextField_Previews: PreviewProvider { + + static var previews: some View { + LabelledUIKitTextField(label: "Label", text: .constant("Text")) + } + +} + + +extension LabelledUIKitTextField { + + init(label: LocalizedStringKey, value: String) { + self.label = label + + _text = .constant("") + + self.placeholder = value + + self.isUserInputEnabled = false + } + +} diff --git a/ui/BankingiOSApp/BankingiOSApp/ui/UIKitTextField.swift b/ui/BankingiOSApp/BankingiOSApp/ui/UIKitTextField.swift index 631a47ff..a86ab995 100644 --- a/ui/BankingiOSApp/BankingiOSApp/ui/UIKitTextField.swift +++ b/ui/BankingiOSApp/BankingiOSApp/ui/UIKitTextField.swift @@ -16,12 +16,18 @@ struct UIKitTextField: UIViewRepresentable { private var focusOnStart = false private var focusNextTextFieldOnReturnKeyPress = false + private var textAlignment: NSTextAlignment = .natural + private var isUserInputEnabled: Bool = true + private var actionOnReturnKeyPress: (() -> Bool)? = nil private var textChanged: ((String) -> Void)? = nil + init(_ titleKey: String, text: Binding, keyboardType: UIKeyboardType = .default, isPasswordField: Bool = false, - focusOnStart: Bool = false, focusNextTextFieldOnReturnKeyPress: Bool = false, actionOnReturnKeyPress: (() -> Bool)? = nil, textChanged: ((String) -> Void)? = nil) { + focusOnStart: Bool = false, focusNextTextFieldOnReturnKeyPress: Bool = false, + textAlignment: NSTextAlignment = .natural, isUserInputEnabled: Bool = true, + actionOnReturnKeyPress: (() -> Bool)? = nil, textChanged: ((String) -> Void)? = nil) { self.placeholder = titleKey _text = text @@ -31,6 +37,9 @@ struct UIKitTextField: UIViewRepresentable { self.focusOnStart = focusOnStart self.focusNextTextFieldOnReturnKeyPress = focusNextTextFieldOnReturnKeyPress + self.textAlignment = textAlignment + self.isUserInputEnabled = isUserInputEnabled + self.actionOnReturnKeyPress = actionOnReturnKeyPress self.textChanged = textChanged } @@ -54,6 +63,8 @@ struct UIKitTextField: UIViewRepresentable { textField.focus() } + textField.textAlignment = textAlignment + return textField } @@ -63,7 +74,8 @@ struct UIKitTextField: UIViewRepresentable { func makeCoordinator() -> UIKitTextField.Coordinator { - return Coordinator(text: $text, focusNextTextFieldOnReturnKeyPress: focusNextTextFieldOnReturnKeyPress, actionOnReturnKeyPress: actionOnReturnKeyPress, textChanged: textChanged) + return Coordinator(text: $text, focusNextTextFieldOnReturnKeyPress: focusNextTextFieldOnReturnKeyPress, isUserInputEnabled: isUserInputEnabled, + actionOnReturnKeyPress: actionOnReturnKeyPress, textChanged: textChanged) } @@ -73,20 +85,29 @@ struct UIKitTextField: UIViewRepresentable { private var focusNextTextFieldOnReturnKeyPress: Bool + private var isUserInputEnabled: Bool + private var actionOnReturnKeyPress: (() -> Bool)? private var textChanged: ((String) -> Void)? - init(text: Binding, focusNextTextFieldOnReturnKeyPress: Bool, actionOnReturnKeyPress: (() -> Bool)? = nil, textChanged: ((String) -> Void)? = nil) { + init(text: Binding, focusNextTextFieldOnReturnKeyPress: Bool, isUserInputEnabled: Bool, + actionOnReturnKeyPress: (() -> Bool)? = nil, textChanged: ((String) -> Void)? = nil) { _text = text self.focusNextTextFieldOnReturnKeyPress = focusNextTextFieldOnReturnKeyPress + self.isUserInputEnabled = isUserInputEnabled self.actionOnReturnKeyPress = actionOnReturnKeyPress self.textChanged = textChanged } + + + func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool { + return isUserInputEnabled + } func textFieldDidChangeSelection(_ textField: UITextField) { let newText = textField.text ?? "" diff --git a/ui/BankingiOSApp/BankingiOSApp/ui/views/AddAccountDialog.swift b/ui/BankingiOSApp/BankingiOSApp/ui/views/AddAccountDialog.swift index 168d4a24..3e779aa8 100644 --- a/ui/BankingiOSApp/BankingiOSApp/ui/views/AddAccountDialog.swift +++ b/ui/BankingiOSApp/BankingiOSApp/ui/views/AddAccountDialog.swift @@ -37,9 +37,9 @@ struct AddAccountDialog: View { } Section { - UIKitTextField("Customer ID", text: $customerId, focusNextTextFieldOnReturnKeyPress: true, actionOnReturnKeyPress: handleReturnKeyPress) + LabelledUIKitTextField(label: "Online banking login name", text: $customerId, focusNextTextFieldOnReturnKeyPress: true, actionOnReturnKeyPress: handleReturnKeyPress) - UIKitTextField("Password", text: $password, isPasswordField: true, actionOnReturnKeyPress: handleReturnKeyPress) + LabelledUIKitTextField(label: "Online banking login password", text: $password, isPasswordField: true, actionOnReturnKeyPress: handleReturnKeyPress) } Section {