简体   繁体   中英

How to get array of fonts contained in an NSFontCollection?

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?

NSArray *familyNames = [[NSFontManager sharedFontManager] availableFontFamilies];

You can get the font collection's matchingDescriptors (an array of NSFontDescriptor ) and ask each descriptor for its -objectForKey:NSFontFamilyAttribute , and dump them (strings) into a set to unique them.

Alternatively you can turn each font descriptor into a font (by creating a font with the descriptor) and ask for its familyName (and then unique those).

Both of these methods are fairly straightforward with Swift's compactMap() (to discard nil s).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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