【发布时间】:2015-01-18 21:02:43
【问题描述】:
我有一个应用程序,用户需要在其中修改样式(字体、大小、粗体、前后颜色……) 如何在 FontPanel 中加载初始样式并在用户验证后获取新属性?
我尝试了很多不同的方法,但都没有成功。
谢谢
【问题讨论】:
标签: cocoa nsfontmanager nsfontpanel
我有一个应用程序,用户需要在其中修改样式(字体、大小、粗体、前后颜色……) 如何在 FontPanel 中加载初始样式并在用户验证后获取新属性?
我尝试了很多不同的方法,但都没有成功。
谢谢
【问题讨论】:
标签: cocoa nsfontmanager nsfontpanel
使用setPanelFont:isMultiple: 设置最初选择的字体。
使用[NSFontManager sharedFontManager] setSelectedAttributes:isMultiple: 更改初始颜色;字典键是 NSForegroundColorAttributeName 和 @"NSDocumentBackgroundColor" 用于颜色,NSUnderlineStyleAttributeName 和 NSStrikethroughStyleAttributeName 用于样式。
当字体改变时,委托实例的changeFont:方法将被调用。
同上样式:changeAttributes: 方法。
以及文本和文档颜色:setColor:forAttribute: 方法。
在changeAttributes方法中获取新属性:
NSDictionary * newAttributes = [sender convertAttributes:@{}];
【讨论】:
斯威夫特 4.2:
NSFontPanel.shared.setPanelFont(myNSFont, isMultiple: false)
NSFontManager.shared.setSelectedAttributes([NSAttributedString.Key.foregroundColor.rawValue: myNSColor], isMultiple: false)
您可以找到所有属性键 here 的列表 - 旧样式常量尚未被弃用,但从 10.13 开始,不再起作用。
如果你想要更改通知,例如,没有附加字段编辑器的标签的内容,你需要设置 NSFontManager.shared 的目标和操作,并注册 NSColorPanel.colorDidChangeNotification (NSColorChanging从 FontPanel 打开 ColorPanel 时不会触发)。
【讨论】: