I have a NSViewRepresentable that wraps an NSTextField subclass which is displayed as larger than your typical text field. SwiftUI doesn't seem to allow me to set the size of the view when the underlying is an NSTextField. It forces it as a single line field.
I've tried both setting the frame on creation as well as using SwiftUI .frame(width:height:) on the represented view.
I always end up with a single line field.
struct BigTextField: NSViewRepresentable {
@Binding var text: String
class Coordinator: NSObject, NSTextFieldDelegate {
var parent: BigTextField
init(_ parent: BigTextField) {
self.parent = parent
}
func controlTextDidChange(_ obj: Notification) {
if let textField = obj.object as? NSTextField {
parent.text = textField.stringValue
}
}
}
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
func makeNSView(context: Context) -> NSTextField {
//let frame = NSRect(x: 0, y: 0, width: 350, height: 140)
//let textField = NSTextField(frame: frame)
let textField = NSTextField()
textField.isEditable = true
textField.isBordered = true
textField.isBezeled = true
textField.delegate = context.coordinator // Assign the coordinator
return textField
}
func updateNSView(_ nsView: NSTextField, context: Context) {
if nsView.stringValue != text {
nsView.stringValue = text
}
}
}
I've also included the SwiftUI declaration which demonstrates the problem.
struct ContentView: View {
@State var text : String = "Test string"
var body: some View {
VStack {
BigTextField(text: $text)
.frame(width: 350, height: 140)
}
.padding()
}
}
NSTextField can be any arbitrary frame size. I already do this from AppKit but am trying to adapt this custom field to work within SwiftUI. SwiftUI seems to override the sizing of this NSViewRepresentable that I give it.
Am I missing something here? Is there some way to override SwiftUI's sizing behavior?
Thank you.
0
0
17