Make NSButton open fonts panel
I was wondering how to go about making a button open up the fonts panel in Mac OS X using Cocoa, I have an开发者_如何学编程 NSTextView in my application and want the fonts dialogue to display when the user clicks the button. Thanks in advance!
The last object that comes by default (those one that you see in the left from the editing area) is exactly the Font Manager. All you have to do is to control drag from your button to the font manager and connect the orderFrontFontPanel:
action.
In Swift (MacOS 10.15, XCode 11), one can add the following method to one's view controller that manages the button:
@IBAction func showFontPanel(_ sender: NSButton)
{
NSFontPanel.shared.orderFront(sender)
}
Then in the storyboard wire the NSButton's action
outlet to that method. The actual name of the method doesn't have to be showFontPanel
. It can be anything else that doesn't conflict with other methods in your view controller.
精彩评论