Reputation: 615
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?
Upvotes: 1
Views: 489
Reputation: 3361
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.
Upvotes: 1