From 0adb32119ab67ff18bc096d39b2784e4da500578 Mon Sep 17 00:00:00 2001 From: Pavel Holec Date: Thu, 15 Feb 2024 13:23:08 +0100 Subject: [PATCH] Add internal keyboard spacing to TextField bottom inset tf --- Sources/Orbit/Components/InputField.swift | 13 +++++++- .../TextFields/InsetableTextField.swift | 32 ++++++++++++++++--- .../Orbit/Support/TextFields/TextField.swift | 6 +++- 3 files changed, 44 insertions(+), 7 deletions(-) diff --git a/Sources/Orbit/Components/InputField.swift b/Sources/Orbit/Components/InputField.swift index 1f55fd23d99..03bde156830 100644 --- a/Sources/Orbit/Components/InputField.swift +++ b/Sources/Orbit/Components/InputField.swift @@ -119,7 +119,8 @@ public struct InputField: isSecureTextEntry: isSecure && isSecureTextRedacted, state: state, leadingPadding: .small, - trailingPadding: .small + trailingPadding: .small, + keyboardSpacing: keyboardSpacing ) .returnKeyType(returnKeyType) .autocorrectionDisabled(isAutocorrectionDisabled) @@ -135,6 +136,12 @@ public struct InputField: isFocused = false inputFieldEndEditingAction() } + // Reverts the additional keyboard spacing used for native keyboard avoidance + .padding(.bottom, -keyboardSpacing) + .overlay( + resolvedPrompt, + alignment: .leadingFirstTextBaseline + ) .accessibility(children: nil) { label } value: { @@ -155,6 +162,10 @@ public struct InputField: } } } + + private var keyboardSpacing: CGFloat { + .medium + } @ViewBuilder private var defaultLabel: some View { switch labelStyle { diff --git a/Sources/Orbit/Support/TextFields/InsetableTextField.swift b/Sources/Orbit/Support/TextFields/InsetableTextField.swift index b878b747da3..9dd98f1ab8b 100644 --- a/Sources/Orbit/Support/TextFields/InsetableTextField.swift +++ b/Sources/Orbit/Support/TextFields/InsetableTextField.swift @@ -3,18 +3,40 @@ import UIKit /// Orbit `UITextField` wrapper with a larger touch area. public class InsetableTextField: UITextField { - // Using .small vertical padding would cause resize issue in secure mode - public var insets = UIEdgeInsets(top: 11, left: 0, bottom: 11, right: 0) + /// Insets for setting overall control touch area. + public var insets = UIEdgeInsets(top: .small, left: 0, bottom: .small, right: 0) { + didSet { + invalidateIntrinsicContentSize() + } + } + + /// Additonal spacing between insets and keyboard. + public var keyboardSpacing: CGFloat = .medium { + didSet { + invalidateIntrinsicContentSize() + } + } + public var shouldDeleteBackwardAction: (String) -> Bool = { _ in true } - + + /// Insets for overall control touch area and native keyboard avoidance. + public var resolvedInsets: UIEdgeInsets { + .init( + top: insets.top, + left: insets.left, + bottom: insets.bottom + keyboardSpacing, + right: insets.right + ) + } + public override func textRect(forBounds bounds: CGRect) -> CGRect { guard Thread.isMainThread else { return .zero } - return super.textRect(forBounds: bounds).inset(by: insets) + return super.textRect(forBounds: bounds).inset(by: resolvedInsets) } public override func editingRect(forBounds bounds: CGRect) -> CGRect { guard Thread.isMainThread else { return .zero } - return super.textRect(forBounds: bounds).inset(by: insets) + return super.textRect(forBounds: bounds).inset(by: resolvedInsets) } public override func deleteBackward() { diff --git a/Sources/Orbit/Support/TextFields/TextField.swift b/Sources/Orbit/Support/TextFields/TextField.swift index 62049beee81..71554f28cba 100644 --- a/Sources/Orbit/Support/TextFields/TextField.swift +++ b/Sources/Orbit/Support/TextFields/TextField.swift @@ -37,6 +37,7 @@ public struct TextField: UIViewRepresentable, TextFieldBuildable { private var state: InputState private var leadingPadding: CGFloat private var trailingPadding: CGFloat + private var keyboardSpacing: CGFloat // Builder properties (keyboard related) var returnKeyType: UIReturnKeyType = .default @@ -69,6 +70,7 @@ public struct TextField: UIViewRepresentable, TextFieldBuildable { uiView.updateIfNeeded(\.insets.left, to: leadingPadding) uiView.updateIfNeeded(\.insets.right, to: trailingPadding) + uiView.updateIfNeeded(\.keyboardSpacing, to: keyboardSpacing) uiView.updateIfNeeded(\.isSecureTextEntry, to: isSecureTextEntry) // Keyboard related @@ -175,13 +177,15 @@ public extension TextField { isSecureTextEntry: Bool = false, state: InputState = .default, leadingPadding: CGFloat = 0, - trailingPadding: CGFloat = 0 + trailingPadding: CGFloat = 0, + keyboardSpacing: CGFloat = 0 ) { self._value = value self.isSecureTextEntry = isSecureTextEntry self.state = state self.leadingPadding = leadingPadding self.trailingPadding = trailingPadding + self.keyboardSpacing = keyboardSpacing } }