简体   繁体   English

如何在可可应用程序中设置默认字体和字体样式?

[英]how to set default font and font style in cocoa app?

My app has all different kind of NSViews, including NSTabView, NSButton, NSTextField, NSPopupButton and so on. 我的应用程序具有所有不同种类的NSView,包括NSTabView,NSButton,NSTextField,NSPopupButton等。

I would like to change all display text to a certain font and with certain font style. 我想将所有显示文本更改为某种字体和某种字体样式。

is there anyway to set the default font and font style in cocoa app? 无论如何,可可应用程序中是否可以设置默认字体和字体样式?

I have tried: How to loop through subviews in order to get the text of NSTextViews 我尝试过: 如何遍历子视图以获取NSTextViews的文本

however it doesn't work with NSTabView and NSButton. 但是它不适用于NSTabView和NSButton。

I think that there must be an easy to do so, it's just I don't know what it is. 我认为这样做一定很容易,只是我不知道这是什么。

Please advise. 请指教。

There isn't a built in way to do this (ie. by setting a key in Info.plist). 没有内置的方法可以执行此操作(即,通过在Info.plist中设置一个键)。 You really do need to go through all the controls in your app, one way or another, and set a custom font for them. 您确实确实需要一种或另一种方式遍历应用程序中的所有控件,并为它们设置自定义字体。 There are a few possible approaches. 有几种可能的方法。 One is to use a custom subclass for all the controls for which you need to use a custom font. 一种是对需要使用自定义字体的所有控件使用自定义子类。 The subclass can be really minimal, it just needs to set the correct font upon initialization, possibly preserving other font attributes (size, color, etc.) setup in Interface Builder. 子类实际上很小,它只需要在初始化时设置正确的字体即可,可能会保留在Interface Builder中设置的其他字体属性(大小,颜色等)。 Another option is to do as you've suggested and go through each subview in your app, check to see if it responds to -setFont: , and set your custom font if it does. 另一个选择是按照您的建议进行操作,并遍历应用程序中的每个子视图,检查其是否对-setFont:作出响应,如果有,请设置自定义字体。 I've used the first approach (subclass) with good success. 我已经成功地使用了第一种方法(子类)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM