NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

SwiftUI and NSFontPanel/NSFontManager

Forums > SwiftUI

I am trying to get NSFontPanel working with SwiftUI using a Document Lifecyle template.

I can get the panel and font, style and size selection working.

Interesting, there is a color picker in the panel, which is not mentioned in any documentation I can find.

I would like to either be able to let the use select a color, and then use it to set the color for my font, in the Label in the sidebar, or if I can't get it to work, I would prefer the color picker not show in the font panel.

Any suggestions would be greatly appreciated.

Click for Screenshot of Fontpanel

public struct FontPicker: View{

    let labelString: String

    @Binding var font: NSFont
    @State var fontPickerDelegate: FontPickerDelegate?

    public init(_ label: String, selection: Binding<NSFont>) {
        self.labelString = label
        self._font = selection

    let fontManager = NSFontManager.shared
    let fontPanel = NSFontPanel.shared

    @AppStorage("setSidebarFont") var setSidebarFont = "System"
    @AppStorage("setSidebarFontSize") var setSidebarFontSize = 24
    @AppStorage("setSidebarFontColor") var setSidebarFontColor = "gray"

    public var body: some View {

        HStack {


            Button {

                if fontPanel.isVisible {

                self.fontPickerDelegate = FontPickerDelegate(self)
       = self.fontPickerDelegate
                fontManager.action = #selector(fontPickerDelegate?.changeAttributes)

                fontPanel.setPanelFont(self.font, isMultiple: false)

            } label: {

                Text("Font Selection: \(setSidebarFont)")
                    .font(.custom(setSidebarFont, size: CGFloat(setSidebarFontSize)))

    func fontSelected() {

        self.font = fontPanel.convert(self.font)
        setSidebarFont = self.font.displayName ?? "System"
        setSidebarFontSize = Int(self.font.pointSize)

        var newAttributes = fontManager.convertAttributes([String : AnyObject]())

        newAttributes["NSForegroundColorAttributeName"] = newAttributes["NSColor"]
        newAttributes["NSUnderlineStyleAttributeName"] = newAttributes["NSUnderline"]
        newAttributes["NSStrikethroughStyleAttributeName"] = newAttributes["NSStrikethrough"]
        newAttributes["NSUnderlineColorAttributeName"] = newAttributes["NSUnderlineColor"]
        newAttributes["NSStrikethroughColorAttributeName"] = newAttributes["NSStrikethroughColor"]


Hacking with Swift is sponsored by Play

SPONSORED Play is the first native iOS design tool created for designers and engineers. You can install Play for iOS and iPad today and sign up to check out the Beta of our macOS app with SwiftUI code export. We're also hiring engineers!

Click to learn more about Play!

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.