Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added expanding TextField #28

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions Sources/SwiftfulUI/Views/ExpandingTextField.swift
Original file line number Diff line number Diff line change
@@ -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

public init(_ placeholder: String, text: Binding<String>) {
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()
}