我有一个应用程序,用户需要在其中修改样式(字体、大小、粗体、前后颜色……)如何在 FontPanel 中加载初始样式并在用户验证后获取新属性?
我尝试了很多不同的东西,但没有任何成功。
谢谢
我有一个应用程序,用户需要在其中修改样式(字体、大小、粗体、前后颜色……)如何在 FontPanel 中加载初始样式并在用户验证后获取新属性?
我尝试了很多不同的东西,但没有任何成功。
谢谢
使用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)
您可以在此处找到所有属性键的列表- 旧样式常量尚未被弃用,但从 10.13 开始,不再有效。
如果您想更改标签的内容,例如,如果您想更改没有附加字段编辑器的内容,您需要设置 NSFontManager.shared 的目标和操作,并注册 NSColorPanel.colorDidChangeNotification(NSColorChanging 不会触发当从 FontPanel 打开 ColorPanel 时)。