Use SwiftUI's Font.largeTitle when NSFont is expected

I am still learning SwiftUI & dev in general. Anything NSWhatever related still throws me off. And I know that my issue is just a lack of understanding 😀

Rn, I am building a SwiftUI macOS 11 app & it works pretty well… but SwiftUI has its limits.

Now, I want to use a NSTextField instead of SwiftUI’s TextField(), bc custom UI etc…

My code:

func makeNSView(context: Context) -> NSTextField {         let textField = FocusAwareTextField()         textField.placeholderAttributedString = NSAttributedString(             string: placeholder,             attributes: [                 NSAttributedString.Key.foregroundColor: NSColor(Color.secondary),                 NSAttributedString.Key.font: NSFont(name: "SF Pro", size: 32)!             ]         )         textField.isBordered = false         textField.delegate = context.coordinator         textField.backgroundColor = NSColor.clear         ...         ...         ... 

My issue: NSAttributedString.Key.font: expects a NSFont.

And the above code builds fine but I’d rather stick to SwiftUI’s Dynamic Font System and use Font.largeTitle.bold() instead of manually defining to use SF Pro and a font size.

I know how to convert Color.black to NSColor(Color.black) but didn’t find a working example for Fonts.

Also, I’d appreciate it if someone could actually explain what is going on so I can understand, instead of just doing copy & paste.

Thank you so much!

Add Comment
0 Answer(s)

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.