cocoansfontnsfontmanager

How do I generate a filtered set of available font family names? (like in Pages or TextEdit)


I want to make a popup button like the one in Pages.app (or in TextEdit.app) whose menu is a filtered list of font families.

I can get an array of available font family names from [NSFontManager availableFontFamilies], but this provides far more font families than are in Pages's popup menu.

On the other hand, I can get what seems to be the correctly filtered set of font descriptors from [NSFontCollection fontCollectionWithName:NSFontCollectionUser], but that includes each member of the family as well (italic, bold, etc). I don't want to include each member in my popup, only the family names.

I want the filtered set of family names.

The names I get out of the NSFontManager seem sanitized for UI, whereas the name values I get out of font descriptor's attributes dictionary ([myFontDescriptor fontAttributes][NSFontNameAttribute]) are not. So I can't just make a simple set intersection of the two groups based on the string values, the same things might have different names.


Solution

  • You can use -[NSFontDescriptor objectForKey:] to retrieve attributes not present in the -fontAttributes dictionary. If you use the attribute key NSFontFamilyAttribute, that gives you the font family name. Applying that to each font descriptor in [NSFontCollection fontCollectionWithName:NSFontCollectionUser] should give you what you need.