import SwiftUI
public struct ExpandingTextFieldView: View {
@Binding var text: String
let placeholder: String
@FocusState private var isFocused: Bool
public init(
text: Binding<String>,
placeholder: String = "Enter your text here..."
) {
self._text = text
self.placeholder = placeholder
}
public var body: some View {
TextEditor(text: $text)
.fixedSize(horizontal: false, vertical: true)
.focused($isFocused)
.onAppear {
text = text.isEmpty ? placeholder : text
}
.onChange(of: isFocused) { _, focus in
text = focus && text == placeholder ? "" : (!focus && text.isEmpty ? placeholder : text)
}
}
}
#Preview {
@State @Previewable var textValue = ""
ExpandingTextFieldView(
text: $textValue,
placeholder: "Type here..."
)
.overlay(RoundedRectangle(cornerRadius: 8).stroke(.gray.opacity(0.5), lineWidth: 2))
}
When the textValue is "", the view renders fine, the placeholder is displayed, and I can edit the value.
When the textValue is set to something else though then the view doesn't render at all. I don't even know how to debug or figure out what's wrong with such a thing. Thank you for your help!
import SwiftUI
public struct ExpandingTextFieldView: View {
@Binding var text: String
let placeholder: String
@FocusState private var isFocused: Bool
public init(
text: Binding<String>,
placeholder: String = "Enter your text here..."
) {
self._text = text
self.placeholder = placeholder
}
public var body: some View {
TextEditor(text: $text)
.fixedSize(horizontal: false, vertical: true)
.focused($isFocused)
.onAppear {
text = text.isEmpty ? placeholder : text
}
.onChange(of: isFocused) { _, focus in
text = focus && text == placeholder ? "" : (!focus && text.isEmpty ? placeholder : text)
}
}
}
#Preview {
@State @Previewable var textValue = ""
ExpandingTextFieldView(
text: $textValue,
placeholder: "Type here..."
)
.overlay(RoundedRectangle(cornerRadius: 8).stroke(.gray.opacity(0.5), lineWidth: 2))
}
When the textValue is "", the view renders fine, the placeholder is displayed, and I can edit the value.
When the textValue is set to something else though then the view doesn't render at all. I don't even know how to debug or figure out what's wrong with such a thing. Thank you for your help!
Share Improve this question asked Jan 18 at 20:10 WokeBlokeWokeBloke 11 bronze badge 4 |1 Answer
Reset to default 0I ended up doing this which worked, but I still want to know why the original way doesn't work.
import SwiftUI
public struct ExpandingTextFieldView: View {
@Binding var text: String
let placeholder: String
public init(
text: Binding<String>,
placeholder: String = "Enter your text here..."
) {
self._text = text
self.placeholder = placeholder
}
public var body: some View {
TextField(placeholder, text: $text, axis: .vertical)
}
}
fixedSixe
modifier that causes the problem but I don't understand why. – Joakim Danielson Commented Jan 18 at 20:42Preview
. I should scroll more often; this is not the first time I've been caught out by this. – workingdog support Ukraine Commented Jan 19 at 8:39