如何设置 NSFontPanel 的默认值?
我正在为我的酷应用程序设置首选项窗口,其中显示一些文本。在首选项中,我设置了一个打开 NSFontPanel
的按钮。我的应用程序存储用户喜欢的文本颜色和字体,并始终使用这些设置打开,以便用户永远不必看到以他们不喜欢的颜色或字体显示的文本。
问题是,虽然我的应用程序能够记住这些首选项,但 NSFontPanel 却遇到了麻烦。当我第一次打开字体面板时,所有字段的默认值都会重置。摆弄它们后,关闭面板,然后重新打开它,正确的值将被保留。仅当我第一次打开面板时才会出现此问题。
我不明白为什么会这样!
当我的应用程序启动时,我一直小心地设置面板的字体和颜色,正如您从以下代码片段中看到的:
def show_entry_font_menu(sender)
font_manager = NSFontManager.sharedFontManager
color_panel = NSColorPanel.sharedColorPanel
font_manager.setDelegate self
color_panel.setDelegate self
font_manager.setSelectedFont(preferences.entry_font, isMultiple:false)
font_panel = font_manager.fontPanel(true)
font_panel.makeKeyAndOrderFront(sender)
attributes = preferences.entry_font_attributes
color = preferences.entry_font_color
font_manager.setSelectedAttributes(attributes, isMultiple:false)
color_panel.setColor(color) if preferences.entry_font_color
self.did_open_font_panel = true
end
I'm setting up the preferences window for my cool app, which displays some text. In preferences I've set up a button that opens an NSFontPanel
. My app stores the user's preferred text color and font, and always opens with those settings so that the user never has to see text displayed in a color or font they don't prefer.
The problem is, while my app is able to remember these preferences, the NSFontPanel
has trouble. When I first open the font panel, the default values for all the fields are reset. After fiddling with them, closing the panel, and then reopening it, the correct values are retained. The problem only occurs when I first open the panel.
I don't understand why this is happening!
I've been careful to set the font and color for the panel when my app starts, as you can see from this snippet:
def show_entry_font_menu(sender)
font_manager = NSFontManager.sharedFontManager
color_panel = NSColorPanel.sharedColorPanel
font_manager.setDelegate self
color_panel.setDelegate self
font_manager.setSelectedFont(preferences.entry_font, isMultiple:false)
font_panel = font_manager.fontPanel(true)
font_panel.makeKeyAndOrderFront(sender)
attributes = preferences.entry_font_attributes
color = preferences.entry_font_color
font_manager.setSelectedAttributes(attributes, isMultiple:false)
color_panel.setColor(color) if preferences.entry_font_color
self.did_open_font_panel = true
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
初始化共享字体面板有一个奇怪的地方。如果您在第一次创建字体之前设置字体,则效果很好,但设置属性则不然。
你要做的就是先makeKeyAndOrderFront,然后设置属性。第一次创建面板后,它将正确反映 setSelectedAttributes。
There is an oddness in initializing the sharedFontPanel. If you set the font before creating it the first time, that works fine, but setting attributes does not.
What you have to do is makeKeyAndOrderFront first, and then set the attributes. Once the panel has been thus created the first time it will properly reflect setSelectedAttributes.