Below is a demo app with a simple NSTextField
in a Mac app. For some reason, the font size won't change no matter what I try.
import Cocoa
import SwiftUI
@main
struct SwiftUIWindowTestApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
struct ContentView: View {
@State private var text = "Text goes here..."
var body: some View {
FancyTextField(text: $text)
.padding(50)
}
}
struct FancyTextField: NSViewRepresentable {
@Binding var text: String
func makeNSView(context: Context) -> NSTextField {
let textField = NSTextField()
textField.font = NSFont.systemFont(ofSize: 30) //<- Not working
return textField
}
func updateNSView(_ nsView: NSTextField, context: Context) {
nsView.stringValue = text
}
}
That's the whole app. I'm not doing anything else in this simple example. I can change the text color and other attributes, but for some reason the font size doesn't change.
On a whim, I tried changing it on the SwiftUI side as well, but I didn't expect that to work (and it doesn't):
FancyTextField(text: $text)
.font(.system(size: 20))
Any ideas?