From 7775685942fdb3bbbc5b9357dd0e9975fa895d2b Mon Sep 17 00:00:00 2001 From: Brendenfrench Date: Thu, 21 Mar 2024 22:12:31 -0500 Subject: [PATCH 1/2] Added multi line text field. --- .../SwiftfulUI/Views/ExpandingTextField.swift | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 Sources/SwiftfulUI/Views/ExpandingTextField.swift diff --git a/Sources/SwiftfulUI/Views/ExpandingTextField.swift b/Sources/SwiftfulUI/Views/ExpandingTextField.swift new file mode 100644 index 0000000..f0df7d7 --- /dev/null +++ b/Sources/SwiftfulUI/Views/ExpandingTextField.swift @@ -0,0 +1,50 @@ +// +// ExpandingTextFieldPreview.swift +// +// +// Created by Brenden French on 3/21/24. +// + +import SwiftUI + +fileprivate struct ExpandingTextFieldPreview: View { + + @State private var text = """ +This is a wrapper around a normal SwiftUI TextField. It expands to fit multiple lines of text because the axis is set to vertical. + +Here is the code for it: +👉 TextField("", text: $text, axis: .vertical) + +Normally this only works on iOS 16 or newer, but this wrapper also uses the single line TextField as a fallback to retain compatibility with older versions of iOS. + +Here is the code for this SwiftfulUI wrapper: +👉 ExpandingTextField("", text: $text) +""" + + var body: some View { + ExpandingTextField("", text: $text) + } +} + +public struct ExpandingTextField: View { + + var placeholder: String + @Binding private var text: String + + init(_ placeholder: String, text: Binding) { + self.placeholder = placeholder + self._text = text + } + + public var body: some View { + if #available(iOS 16.0, *) { + TextField(placeholder, text: $text, axis: .vertical) + } else { + TextField(placeholder, text: $text) + } + } +} + +#Preview{ + ExpandingTextFieldPreview() +} From 1d45ceeede501868c547b4535a6ef7033bcaf6bf Mon Sep 17 00:00:00 2001 From: Brendenfrench Date: Thu, 21 Mar 2024 23:11:20 -0500 Subject: [PATCH 2/2] changed init for ExpandingTextField to public --- Sources/SwiftfulUI/Views/ExpandingTextField.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/SwiftfulUI/Views/ExpandingTextField.swift b/Sources/SwiftfulUI/Views/ExpandingTextField.swift index f0df7d7..8738b75 100644 --- a/Sources/SwiftfulUI/Views/ExpandingTextField.swift +++ b/Sources/SwiftfulUI/Views/ExpandingTextField.swift @@ -31,7 +31,7 @@ public struct ExpandingTextField: View { var placeholder: String @Binding private var text: String - init(_ placeholder: String, text: Binding) { + public init(_ placeholder: String, text: Binding) { self.placeholder = placeholder self._text = text }