From e42e06e6da9ce3757f9abdea667666ed8edfe078 Mon Sep 17 00:00:00 2001 From: Mikhail Date: Thu, 27 Mar 2025 14:23:47 +0100 Subject: [PATCH 1/3] improve method for calculating adapted corner radius for the text input --- .../TextInput/Models/TextInputVM.swift | 32 +++++++------------ .../Components/TextInput/SUTextInput.swift | 2 +- .../Components/TextInput/UKTextInput.swift | 7 ++-- 3 files changed, 14 insertions(+), 27 deletions(-) diff --git a/Sources/ComponentsKit/Components/TextInput/Models/TextInputVM.swift b/Sources/ComponentsKit/Components/TextInput/Models/TextInputVM.swift index db000784..274cce4b 100644 --- a/Sources/ComponentsKit/Components/TextInput/Models/TextInputVM.swift +++ b/Sources/ComponentsKit/Components/TextInput/Models/TextInputVM.swift @@ -69,23 +69,6 @@ public struct TextInputVM: ComponentVM { // MARK: - Shared Helpers extension TextInputVM { - var adaptedCornerRadius: ComponentRadius { - switch self.cornerRadius { - case .none: - return .none - case .small: - return .small - case .medium: - return .medium - case .large: - return .large - case .full: - return .custom(self.height(forRows: 1) / 2) - case .custom(let value): - return .custom(value) - } - } - var preferredFont: UniversalFont { if let font { return font @@ -140,6 +123,17 @@ extension TextInputVM { } } + func adaptedCornerRadius(for height: CGFloat = 10_000) -> CGFloat { + switch self.cornerRadius { + case .none, .small, .medium, .large, .full: + let value = self.cornerRadius.value(for: height) + let maxValue = ComponentRadius.custom(self.height(forRows: 1) / 2).value(for: height) + return min(value, maxValue) + case .custom(let value): + return ComponentRadius.custom(value).value(for: height) + } + } + private func height(forRows rows: Int) -> CGFloat { if rows < 1 { assertionFailure("Number of rows in TextInput must be greater than or equal to 1") @@ -162,8 +156,4 @@ extension TextInputVM { var autocorrectionType: UITextAutocorrectionType { return self.isAutocorrectionEnabled ? .yes : .no } - - func shouldUpdateCornerRadius(_ oldModel: Self) -> Bool { - return self.adaptedCornerRadius != oldModel.adaptedCornerRadius - } } diff --git a/Sources/ComponentsKit/Components/TextInput/SUTextInput.swift b/Sources/ComponentsKit/Components/TextInput/SUTextInput.swift index 3616297c..ca95da09 100644 --- a/Sources/ComponentsKit/Components/TextInput/SUTextInput.swift +++ b/Sources/ComponentsKit/Components/TextInput/SUTextInput.swift @@ -116,7 +116,7 @@ public struct SUTextInput: View { ) .clipShape( RoundedRectangle( - cornerRadius: self.model.adaptedCornerRadius.value() + cornerRadius: self.model.adaptedCornerRadius() ) ) } diff --git a/Sources/ComponentsKit/Components/TextInput/UKTextInput.swift b/Sources/ComponentsKit/Components/TextInput/UKTextInput.swift index 1e539bc0..8e4b8519 100644 --- a/Sources/ComponentsKit/Components/TextInput/UKTextInput.swift +++ b/Sources/ComponentsKit/Components/TextInput/UKTextInput.swift @@ -118,9 +118,6 @@ open class UKTextInput: UIView, UKComponent { self.style() - if self.model.shouldUpdateCornerRadius(oldModel) { - self.updateCornerRadius() - } if self.model.shouldUpdateLayout(oldModel) { self.invalidateIntrinsicContentSize() self.setNeedsLayout() @@ -171,7 +168,7 @@ open class UKTextInput: UIView, UKComponent { } private func updateCornerRadius() { - self.layer.cornerRadius = self.model.adaptedCornerRadius.value(for: self.bounds.height) + self.layer.cornerRadius = self.model.adaptedCornerRadius(for: self.bounds.height) } } @@ -189,7 +186,7 @@ extension UKTextInput { fileprivate enum Style { static func mainView(_ view: UIView, model: TextInputVM) { view.backgroundColor = model.backgroundColor.uiColor - view.layer.cornerRadius = model.adaptedCornerRadius.value(for: view.bounds.height) + view.layer.cornerRadius = model.adaptedCornerRadius(for: view.bounds.height) } static func textView( From 4b98a2a39465426fdd70371aa76d2e92860f1521 Mon Sep 17 00:00:00 2001 From: Mikhail Date: Thu, 27 Mar 2025 14:36:35 +0100 Subject: [PATCH 2/3] recalculate text input height when device rotates --- .../ComponentsKit/Components/TextInput/SUTextInput.swift | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Sources/ComponentsKit/Components/TextInput/SUTextInput.swift b/Sources/ComponentsKit/Components/TextInput/SUTextInput.swift index ca95da09..44019dc0 100644 --- a/Sources/ComponentsKit/Components/TextInput/SUTextInput.swift +++ b/Sources/ComponentsKit/Components/TextInput/SUTextInput.swift @@ -112,6 +112,13 @@ public struct SUTextInput: View { ) } } + .onChange(of: geometry.size.width) { newValue in + self.textEditorPreferredHeight = TextInputHeightCalculator.preferredHeight( + for: self.text, + model: self.model, + width: newValue + ) + } } ) .clipShape( From e539c85424a2f387ddc88f15ec1700443e27168b Mon Sep 17 00:00:00 2001 From: Mikhail Date: Thu, 27 Mar 2025 14:38:19 +0100 Subject: [PATCH 3/3] make text input resistant to compression --- .../Components/TextInput/SUTextInput.swift | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/Sources/ComponentsKit/Components/TextInput/SUTextInput.swift b/Sources/ComponentsKit/Components/TextInput/SUTextInput.swift index 44019dc0..47e68014 100644 --- a/Sources/ComponentsKit/Components/TextInput/SUTextInput.swift +++ b/Sources/ComponentsKit/Components/TextInput/SUTextInput.swift @@ -55,16 +55,14 @@ public struct SUTextInput: View { TextEditor(text: self.$text) .contentMargins(self.model.contentPadding) .transparentScrollBackground() - .frame( - minHeight: self.model.minTextInputHeight, - maxHeight: max( - self.model.minTextInputHeight, - min( - self.model.maxTextInputHeight, - self.textEditorPreferredHeight - ) + .frame(minHeight: self.model.minTextInputHeight) + .frame(height: max( + self.model.minTextInputHeight, + min( + self.model.maxTextInputHeight, + self.textEditorPreferredHeight ) - ) + )) .lineSpacing(0) .font(self.model.preferredFont.font) .foregroundStyle(self.model.foregroundColor.color)