1

I am wondering what fonts are guaranteed to be available on an iOS device.

I used this and it works fine:

[[UITabBarItem appearance] setTitleTextAttributes:
 [NSDictionary dictionaryWithObjectsAndKeys:
  [UIColor grayColor],      UITextAttributeTextColor,
  [UIColor lightGrayColor], UITextAttributeTextShadowColor,
  [NSValue valueWithUIOffset:UIOffsetMake(1, 1)], UITextAttributeTextShadowOffset,
  [UIFont fontWithName:@"Helvetica Bold" size:0.0], UITextAttributeFont,
  nil]
                                         forState:UIControlStateNormal];

I then thought it might be better and more convenient to use the new syntax for a dictionary so I tried this:

[[UITabBarItem appearance] setTitleTextAttributes:@{
                             UITextAttributeFont : [UIFont fontWithName:@"Helvetica-Bold" size:0.0f],
                        UITextAttributeTextColor : [UIColor grayColor],
                  UITextAttributeTextShadowColor : [UIColor lightGrayColor],
                 UITextAttributeTextShadowOffset : [NSValue valueWithUIOffset:UIOffsetMake(1.0f, 1.0f)],
 } forState:UIControlStateNormal];

It does work, but there are some issues I am concerned about.

Initially, I had tried this font, [UIFont fontWithName:@"Helvetica Bold" size:0.0]. That font actually does not exist so the returned UIFont is nil.

The old approach using dictionaryWithObjectsAndKeys handled the nil font just fine, but the new @{} syntax crashes the app.

I was able to fix that by using the correct font name, @"Helvetica-Bold" (needed a hyphen), but I am concerned about selecting a font and using this syntax if there is a possibility that a device won't have a selected font available and crashes.

Since the old approach using dictionaryWithObjectsAndKeys works fine, and apparently can handle a nil font, I am inclined to just use that and sleep easy at night.

I certainly could put in logic to check for a nil font and take steps to handle that, but seems like a needless waste of time since the old approach handles that situation.

Would it be better to just always use the old syntax since there is no telling what fonts might be available on future devices?

Somewhat disappointed with Apple over this problem.

Any thoughts, opinions, or suggestions?

GRW
  • 605
  • 12
  • 27
  • 1
    Take a look at [iosfonts.com](http://iosfonts.com) – iHunter Aug 29 '13 at 20:04
  • or here: http://daringfireball.net/misc/2007/07/iphone-osx-fonts – Kiefer Aguilar Aug 29 '13 at 20:30
  • Both of those sites look great, and I have kept links to them for future reference, however I do not think they will help me with this issue. See my response to the answer from Hetal Vora below for why. – GRW Aug 30 '13 at 01:24

1 Answers1

1

You can use

NSArray *familyNames = [UIFont familyNames];

Then for each familyName in the array, you can see the available fontNames as:

 NSArray *fontNames = [UIFont fontNamesForFamilyName:familyName];

This will give you the list of font names available on the device.

Hetal Vora
  • 3,341
  • 2
  • 28
  • 53
  • I don't think that's what he is asking. I think he wants to know a font that is universally used in every device. – El Tomato Aug 29 '13 at 22:21
  • I'm looking for something that is always guaranteed to be available on any device running my app forever so I can safely use the new dictionary syntax @{}. Since that is probably impossible to guarantee, I think I will probably go back to using the old format with dictionaryWithObjectsAndKeys. – GRW Aug 30 '13 at 01:18