UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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 {

            Text(labelString)

            Button {

                if fontPanel.isVisible {
                    fontPanel.orderOut(nil)
                    return
                }

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

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

            } 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"]
        print("\(newAttributes["NSForegroundColorAttributeName"]!)")
    }
}

2      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.