Skip to content

Commit

Permalink
Add internal keyboard spacing to TextField bottom inset
Browse files Browse the repository at this point in the history
tf
  • Loading branch information
PavelHolec committed Nov 15, 2024
1 parent f822178 commit 0adb321
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
13 changes: 12 additions & 1 deletion Sources/Orbit/Components/InputField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ public struct InputField<Label: View, Prompt: View, Prefix: View, Suffix: View>:
isSecureTextEntry: isSecure && isSecureTextRedacted,
state: state,
leadingPadding: .small,
trailingPadding: .small
trailingPadding: .small,
keyboardSpacing: keyboardSpacing
)
.returnKeyType(returnKeyType)
.autocorrectionDisabled(isAutocorrectionDisabled)
Expand All @@ -135,6 +136,12 @@ public struct InputField<Label: View, Prompt: View, Prefix: View, Suffix: View>:
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: {
Expand All @@ -155,6 +162,10 @@ public struct InputField<Label: View, Prompt: View, Prefix: View, Suffix: View>:
}
}
}

private var keyboardSpacing: CGFloat {
.medium
}

@ViewBuilder private var defaultLabel: some View {
switch labelStyle {
Expand Down
32 changes: 27 additions & 5 deletions Sources/Orbit/Support/TextFields/InsetableTextField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
6 changes: 5 additions & 1 deletion Sources/Orbit/Support/TextFields/TextField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
}

Expand Down

0 comments on commit 0adb321

Please sign in to comment.