Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

cocoa

fonts

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?

like image 534
Arne Evertsson Avatar asked Mar 12 '10 13:03

Arne Evertsson


2 Answers

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?

like image 34
ericg Avatar answered Sep 28 '22 04:09

ericg


This works:

NSFontManager *fontManager = [NSFontManager sharedFontManager];
NSFont *boldItalic = [fontManager fontWithFamily:@"Verdana"
                                          traits:NSBoldFontMask|NSItalicFontMask
                                          weight:0
                                            size:75];
like image 161
Arne Evertsson Avatar answered Sep 28 '22 03:09

Arne Evertsson