1

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 :D

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!

  • Does this answer your question? [How to make NSMutableAttributedString responsive with dynamic type text from settings app](https://stackoverflow.com/questions/47568987/how-to-make-nsmutableattributedstring-responsive-with-dynamic-type-text-from-set) – koen Aug 20 '20 at 12:10
  • It seems all SwiftUI specific types are meant for UI purposes only. There is no way to convert a Font to a CTFont, UIFont or NSFont. You cannot even get any font properties. The same holds for Color. My piece of advice would be to stick with the NSWhatever types for your data and convert to SwiftUI types when displaying. In case you need a largeTitle in your attributedString, just use `preferredFont(forTextStyle:options:)`. Note that you *can* easily convert other types to SwiftUI types. – Jack Goossen Aug 25 '20 at 10:31

0 Answers0