7

I would like to make a NSFont to describe Arial, normal, 30pt in height. So far I have:

    NSNumber *weight = [NSNumber numberWithFloat:1.0];
    NSNumber *slant = [NSNumber numberWithFloat:1.0];
    NSDictionary *fontTraits = [NSDictionary dictionaryWithObjectsAndKeys: weight, NSFontWeightTrait, slant, NSFontSlantTrait, nil];
    NSDictionary *fontAttributes = [NSDictionary dictionaryWithObjectsAndKeys: @"Arial", NSFontFaceAttribute, 
                                                                               fontTraits, NSFontTraitsAttribute, nil];
    NSFontDescriptor *fontDescriptor = [NSFontDescriptor fontDescriptorWithFontAttributes: fontAttributes];
    NSFont *largeFont = [NSFont fontWithDescriptor: fontDescriptor size: 30];

but the resulting NSFont is not the right size. I can put any size I want in there and they all look the same.

Justin808
  • 20,859
  • 46
  • 160
  • 265

1 Answers1

10

are you writing for iOS or Mac OS X?

this works fine in my Mac App:

NSFont* font = [NSFont fontWithName:@"Arial" size:30];

UPDATE with bold and/or italic: is that enough for you ?

NSFont* font = [NSFont fontWithName:@"Arial Italic" size:30];
NSFont* font = [NSFont fontWithName:@"Arial Bold" size:30];
NSFont* font = [NSFont fontWithName:@"Arial Bold Italic" size:30];

UPDATE 2 may be take a look at NSFontManager

// convert font
NSFont* font = [NSFont fontWithName:@"Arial" size:30];
font = [[NSFontManager sharedFontManager] convertFont:font toHaveTrait:NSFontItalicTrait];

// create with traits and weight
NSFont* font = [[NSFontManager sharedFontManager]  fontWithFamily:@"Arial" traits:NSFontItalicTrait weight:2 size:30];

But with neither of those you are able to create a font with a weight of 5 and a slant of 4.

i recently talked to a designer about fonts and he told me that usually a font like Arial is actually 4 Fonts (i.e. Arial, Arial Italic, Arial Bold and Arial Bold Italic). those 3 other font styles (with the traits) are not generated on the fly by an algorithm.

JeanLuc
  • 4,783
  • 1
  • 33
  • 47
  • This works, but I can't change the weight or slant with this method of doing things. – Justin808 Jan 24 '12 at 21:29
  • is that enough for you ? or do you really want the dynamic weight and slant ? – JeanLuc Jan 24 '12 at 22:39
  • When using some hardcoded font name, have a fallback ready for using SystemFontOfSize, in case it is not present on the users system. Even if the font name is "Arial". We once had a support issue because the user didn't have "Verdana", for some weird reason. – Radu Simionescu Feb 09 '16 at 14:48