开发者

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.

Make NSButton open fonts panel

Make NSButton open fonts panel


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜