Implemented LabelledUIKitTextField that displays a label on the left and a text field on the right

This commit is contained in:
dankito 2020-08-08 18:21:36 +02:00
parent 8aa38008d4
commit fa9af8155a
5 changed files with 92 additions and 9 deletions

View File

@ -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%@";

View File

@ -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%@";

View File

@ -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
}
}

View File

@ -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<String>, 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,21 +85,30 @@ struct UIKitTextField: UIViewRepresentable {
private var focusNextTextFieldOnReturnKeyPress: Bool
private var isUserInputEnabled: Bool
private var actionOnReturnKeyPress: (() -> Bool)?
private var textChanged: ((String) -> Void)?
init(text: Binding<String>, focusNextTextFieldOnReturnKeyPress: Bool, actionOnReturnKeyPress: (() -> Bool)? = nil, textChanged: ((String) -> Void)? = nil) {
init(text: Binding<String>, 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 ?? ""
let didTextChange = newText != text // e.g. if just the cursor has been placed to another position then textFieldDidChangeSelection() gets called but text didn't change

View File

@ -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 {