Implemented that a tap on label focuses text field

This commit is contained in:
dankito 2020-08-10 00:21:39 +02:00
parent 00d7b7d24d
commit f05a551bf1
2 changed files with 30 additions and 3 deletions

View File

@ -15,6 +15,18 @@ struct LabelledUIKitTextField: View {
var focusOnStart = false var focusOnStart = false
var focusNextTextFieldOnReturnKeyPress = false var focusNextTextFieldOnReturnKeyPress = false
@State var focusTextField: Bool = false
private var focusTextFieldBinding: Binding<Bool> {
Binding<Bool>(
get: {print("Getting focusTextFieldBinding for \(self.focusTextField)")
return self.focusTextField },
set: {
self.focusTextField = $0
})
}
var isUserInputEnabled: Bool = true var isUserInputEnabled: Bool = true
var actionOnReturnKeyPress: (() -> Bool)? = nil var actionOnReturnKeyPress: (() -> Bool)? = nil
@ -25,11 +37,16 @@ struct LabelledUIKitTextField: View {
var body: some View { var body: some View {
HStack { HStack {
Text(label) Text(label)
.onTapGesture {
DispatchQueue.main.async {
self.focusTextField = true
}
}
Spacer() Spacer()
UIKitTextField(placeholder, text: $text, keyboardType: keyboardType, isPasswordField: isPasswordField, UIKitTextField(placeholder, text: $text, keyboardType: keyboardType, isPasswordField: isPasswordField,
focusOnStart: focusOnStart, focusNextTextFieldOnReturnKeyPress: focusNextTextFieldOnReturnKeyPress, focusOnStart: focusOnStart, focusNextTextFieldOnReturnKeyPress: focusNextTextFieldOnReturnKeyPress, focusTextField: focusTextFieldBinding,
textAlignment: .right, isUserInputEnabled: isUserInputEnabled, textAlignment: .right, isUserInputEnabled: isUserInputEnabled,
actionOnReturnKeyPress: actionOnReturnKeyPress, textChanged: textChanged) actionOnReturnKeyPress: actionOnReturnKeyPress, textChanged: textChanged)
} }

View File

@ -15,6 +15,7 @@ struct UIKitTextField: UIViewRepresentable {
private var focusOnStart = false private var focusOnStart = false
private var focusNextTextFieldOnReturnKeyPress = false private var focusNextTextFieldOnReturnKeyPress = false
@Binding private var focusTextField: Bool
private var textAlignment: NSTextAlignment = .natural private var textAlignment: NSTextAlignment = .natural
private var isUserInputEnabled: Bool = true private var isUserInputEnabled: Bool = true
@ -25,7 +26,7 @@ struct UIKitTextField: UIViewRepresentable {
init(_ titleKey: String, text: Binding<String>, keyboardType: UIKeyboardType = .default, isPasswordField: Bool = false, init(_ titleKey: String, text: Binding<String>, keyboardType: UIKeyboardType = .default, isPasswordField: Bool = false,
focusOnStart: Bool = false, focusNextTextFieldOnReturnKeyPress: Bool = false, focusOnStart: Bool = false, focusNextTextFieldOnReturnKeyPress: Bool = false, focusTextField: Binding<Bool> = .constant(false),
textAlignment: NSTextAlignment = .natural, isUserInputEnabled: Bool = true, textAlignment: NSTextAlignment = .natural, isUserInputEnabled: Bool = true,
actionOnReturnKeyPress: (() -> Bool)? = nil, textChanged: ((String) -> Void)? = nil) { actionOnReturnKeyPress: (() -> Bool)? = nil, textChanged: ((String) -> Void)? = nil) {
self.placeholder = titleKey self.placeholder = titleKey
@ -36,6 +37,7 @@ struct UIKitTextField: UIViewRepresentable {
self.focusOnStart = focusOnStart self.focusOnStart = focusOnStart
self.focusNextTextFieldOnReturnKeyPress = focusNextTextFieldOnReturnKeyPress self.focusNextTextFieldOnReturnKeyPress = focusNextTextFieldOnReturnKeyPress
self._focusTextField = focusTextField
self.textAlignment = textAlignment self.textAlignment = textAlignment
self.isUserInputEnabled = isUserInputEnabled self.isUserInputEnabled = isUserInputEnabled
@ -69,7 +71,15 @@ struct UIKitTextField: UIViewRepresentable {
} }
func updateUIView(_ uiView: UITextField, context: UIViewRepresentableContext<UIKitTextField>) { func updateUIView(_ uiView: UITextField, context: UIViewRepresentableContext<UIKitTextField>) {
uiView.text = text uiView.text = text
if focusTextField {
uiView.focus()
DispatchQueue.main.async {
self.focusTextField = false // reset value so that it can be set again (otherwise it may never gets resetted and then updateUIView() requests focus even though already another view got the focus in the meantime)
}
}
} }