简体   繁体   English

修改NSFont的惯用方式

[英]Idiomatic way of modifying NSFont

I have a NSFont whose size, family, traits I would like to modify. 我有一个NSFont ,我想修改其大小,家庭,特征。

I found that I can use NSFontManager to do so (using eg convertFont:toFamily or convertFont:toSize ), but I suspected that there would be some possibility to alter all at once (I imagined something like convertFont:toFamily:toSize:toHaveTrait ). 我发现我可以使用NSFontManager来做到这一点(例如使用convertFont:toFamilyconvertFont:toSize ),但是我怀疑有可能一次全部更改(我想像的是convertFont:toFamily:toSize:toHaveTrait )。 However, I have not found any such thing, which is why I am currently doing a stepwise transformation. 但是,我还没有找到任何类似的东西,这就是为什么我目前正在逐步进行转换。

Another thing I have considered is using a NSFontDescriptor . 我考虑过的另一件事是使用NSFontDescriptor However, using this seems a bit cumbersome if I already start with an NSFont ( NSFont -> NSFontDescriptor -> modified NSFontDescriptor -> modified NSFont looks more verbose to me than NSFont -> converted NSFont ). 但是,如果我已经开始使用NSFont,使用它似乎有点麻烦( NSFont -> NSFontDescriptor -> modified NSFontDescriptor -> modified NSFont在我看来比NSFont -> converted NSFont NSFont -> NSFontDescriptor -> modified NSFontDescriptor -> modified NSFont更冗长)。

So, is there an ideomatic way of converting NSFont s? 那么,是否存在一种转换NSFont And if so, what is it? 如果是这样,那是什么?

Font descriptors are the usual way to approach this. 字体描述符是解决此问题的常用方法。 See Using Fonts with Text Kit from WWDC 2013 starting around 26:00 (this talk is focused on UIKit, but many of the principles apply to AppKit as well). 请参阅从26:00左右开始的WWDC 2013中的将字体与文本工具包一起使用 (此演讲的重点是UIKit,但是许多原理也适用于AppKit)。 NSFontManager is designed to work with an interactive font-picker UI (particularly the Font Panel). NSFontManager旨在与交互式字体选择器UI(特别是“字体面板”)一起使用。 If you're interacting with the user-selected font (in a UI-kind of way), you should definitely use NSFontManager , but in most cases then you're modifying things piecemeal as the user selects and deselects various options. 如果要与用户选择的字体进行交互(以某种UI的方式),则一定要使用NSFontManager ,但是在大多数情况下,您会随着用户选择和取消选择各种选项而零星地进行修改。 If you're manipulating fonts independently of the user, you probably want to use font descriptors, particularly so you don't unintentionally modify the font panel. 如果要独立于用户来操作字体,则可能要使用字体描述符,尤其是这样,您就不会无意间修改了字体面板。

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

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