1

How can you get an NSArray of fonts or fontfamilies that are "contained" by an NSFontCollection?

All NSFont and NSFontManager methods seem to require a single NSFontDescriptor, while NSFontCollection returns an NSArray of NSFontDescriptor objects. It seems like there should be a method that returns fonts matching these.

Is it necessary to query each font descriptor against all fonts manually?

4

2 回答 2

0
NSArray *familyNames = [[NSFontManager sharedFontManager] availableFontFamilies];
于 2014-05-28T11:58:56.853 回答
0

您可以获取字体集合matchingDescriptors(一个数组NSFontDescriptor)并询问每个描述符的-objectForKey:NSFontFamilyAttribute,并将它们(字符串)转储到一个集合中以使它们唯一。

或者,您可以将每个字体描述符转换为字体(通过使用描述符创建字体)并请求它familyName(然后将其唯一)。

这两种方法对于 Swift 来说都相当简单compactMap()(丢弃nils)。

于 2020-04-16T19:44:38.373 回答