tags:

views:

571

answers:

4

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?

+5  A: 

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
+3  A: 

See NSFontManager and -convertFont:toHaveTrait:

For more detailed information, I would suggest reading the Font Handling Guide ( http://bit.ly/ak5JGE ) and specifically the section titled Converting Fonts Manually ( http://bit.ly/bYawXs ).

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:

http://stackoverflow.com/questions/1724647/how-do-i-get-lucida-grande-italic-into-my-application \

ericgorr
+1  A: 

This works:

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

NSFont undeclared

dimple
Hi dimple. Welcome to StackOverflow. This is a question and answer site, and you have posted this as an answer to Arne Evertsson's question. If you have a specific question about NSFont, you should use the "Ask a Question" button at the top of the page. It would also be a good idea to provide more information about the problem. Cheers!
e.James