12

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?

Arne Evertsson
  • 19,693
  • 20
  • 69
  • 84

4 Answers4

28

This works:

NSFontManager *fontManager = [NSFontManager sharedFontManager];
NSFont *boldItalic = [fontManager fontWithFamily:@"Verdana"
                                          traits:NSBoldFontMask|NSItalicFontMask
                                          weight:0
                                            size:75];
Arne Evertsson
  • 19,693
  • 20
  • 69
  • 84
  • 1
    I apologize for the obvious plug but I wanted to mention that I've made the [CocoaPuffs](http://github.com/macpgrog-guy/CocoaPuffs) framework available on GitHub and it contains a nice category for working with NSFont and NSFontManager. There are also some details [here](http://macprog.com/simplifying-nsfont/). In this specific case, if you already had a reference to the Verdana font you all you would have needed to do is verdana.fontVariationBoldItalic. – aLevelOfIndirection Oct 10 '12 at 12:01
  • @aLevelOfIndirection : the two links you gave us are broken. – Vince Feb 26 '15 at 09:59
  • Apologies. I've since taken down the framework. I've added a [Gist](https://gist.github.com/macprog-guy/156d33bfefef570a7efb) just now with the code for it. So go ahead and download it. – aLevelOfIndirection Feb 26 '15 at 10:24
  • In addition, if you want 'regular weight', `weight` should be `5`, here more details: https://developer.apple.com/documentation/appkit/nsfontmanager/1462332-font – vicegax Apr 12 '22 at 09:38
10

See NSFontManager and -convertFont:toHaveTrait:

For more detailed information, I would suggest reading the Font Handling Guide and specifically the section titled Converting Fonts Manually.

Note, that the font you are using needs to have some version of it with the traits you are asking for, otherwise, you will get back a font, but without the requested trait.

If, eventually, you are seeking to add an italic trait to a font that doesn't have one, check out:

How do I get Lucida Grande italic into my application?

Shebuka
  • 3,148
  • 1
  • 26
  • 43
ericg
  • 8,413
  • 9
  • 43
  • 77
7

Verdana-BoldItalic.

(The actual name of the bold-italic variant of the font depends on the family, and some font doesn't have bold-italic, Use a NSFontDescriptor with -fontDescriptorWithSymbolicTraits: to get the exact bold italic font.)

kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
1

If you are using Swift instead:

let font: NSFont? = NSFontManager.shared.font(
  withFamily: "Verdana",
  traits: [.boldFontMask, .italicFontMask],
  weight: 5,
  size: 12
)
Phoenix Himself
  • 1,037
  • 10
  • 17