Search code examples
cocoafonts

How do I create an NSFont that is both Bold and Italic?


This is a beginner's question about font handling in Cocoa. I have a font family, e.g. Verdana, that has the following typefaces: Regular, Bold, Italic, and Bold Italic. I know those typefaces exist since they are available in the Fonts panel.

This works:

NSFont *regular = [NSFont fontWithName:@"Verdana" size:75];
NSFont *bold = [NSFont fontWithName:@"Verdana-Bold" size:75];
NSFont *italic = [NSFont fontWithName:@"Verdana-Italic" size:75];

This does not work:

NSFont *boldItalic = [NSFont fontWithName:@"Verdana-Bold Italic" size:75];

What is the simplest way to get the Bold Italic version of a given font family?


Solution

  • This works:

    NSFontManager *fontManager = [NSFontManager sharedFontManager];
    NSFont *boldItalic = [fontManager fontWithFamily:@"Verdana"
                                              traits:NSBoldFontMask|NSItalicFontMask
                                              weight:0
                                                size:75];