I have not tested this but give it a try
SB
import SwiftUI
struct TextFieldDynamicHeight: ViewModifier {
var fullSize: Binding<Bool> = .constant(false)
var upperRange: ClosedRange<Int>
var lowerRange: ClosedRange<Int>
var upperBound: Int
var lowerBound: Int
func body(content: Content) -> some View {
if #available(iOS 16.0, *) {
content
.lineLimit(fullSize.wrappedValue ? upperRange : lowerRange)
} else {
content
.lineLimit(fullSize.wrappedValue ? upperBound : lowerBound)
}
}
}
extension View {
@available(iOS 16.0, macOS 13.0, tvOS 16.0, watchOS 9.0, *)
func lineLimit(_ fullSize: Binding<Bool>, upperRange: ClosedRange<Int>, lowerRange: ClosedRange<Int>) -> some View {
modifier(TextFieldDynamicHeight(fullSize: fullSize, upperRange: upperRange, lowerRange: lowerRange,
upperBound: 0, lowerBound: 0))
}
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
func lineLimit(_ fullSize: Binding<Bool>, upperBound: Int, lowerBound: Int) -> some View {
modifier(TextFieldDynamicHeight(fullSize: fullSize, upperRange: 0...0, lowerRange: 0...0,
upperBound: upperBound, lowerBound: lowerBound))
}
}
struct ContentView: View {
@State private var description: String = ""
@State private var fullSize: Bool = false
@State private var fullHeight: Bool = false
var body: some View {
VStack {
VStack {
Image(systemName: "globe")
.imageScale(.large)
.foregroundColor(.accentColor)
Text("Hello world!")
}
if #available(iOS 16.0, *) {
TextField("Type a textmail", text: $description, axis: .vertical)
.lineLimit($fullSize, upperRange: 2...6, lowerRange: 2...3)
.foregroundColor(.white)
.background(Color.blue)
.frame(height: fullHeight ? 300 : 100)
.padding(0)
} else {
TextField("Type a textmail", text: $description)
.lineLimit($fullSize, upperBound:6, lowerBound: 3)
.foregroundColor(.white)
.background(Color.blue)
.frame(height: fullHeight ? 300 : 100)
.padding(0)
}
Button("Full size") {
fullSize.toggle()
print("fullSize: \(fullSize)")
}
.padding(5)
Button("Full height") {
fullHeight.toggle()
print("fullHeight: \(fullHeight)")
}
.padding(5)
}
}
}