Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if font exist in iOS4 at runtime

I wish to check at runtime whether a font exists on the device (iphone/ipad)?

Is there a way to do that?

like image 612
shannoga Avatar asked May 30 '11 17:05

shannoga


2 Answers

you could try by using the UIFont fontWithName method , i feel it should return nil for the font which does'nt exist in iOS.

+ (UIFont *)fontWithName:(NSString *)fontName size:(CGFloat)fontSize
like image 68
Jhaliya - Praveen Sharma Avatar answered Oct 27 '22 04:10

Jhaliya - Praveen Sharma


You can use this code to get a list of all fonts available:

// List all fonts on iPhone
NSArray *familyNames = [[NSArray alloc] initWithArray:[UIFont familyNames]];
NSArray *fontNames;
NSInteger indFamily, indFont;
for (indFamily=0; indFamily<[familyNames count]; ++indFamily)
{
  NSLog(@"Family name: %@", [familyNames objectAtIndex:indFamily]);
  fontNames = [[NSArray alloc] initWithArray:
      [UIFont fontNamesForFamilyName:
      [familyNames objectAtIndex:indFamily]]];
  for (indFont=0; indFont<[fontNames count]; ++indFont)
  {
      NSLog(@"    Font name: %@", [fontNames objectAtIndex:indFont]);
  }
  [fontNames release];
}
[familyNames release];

Based on this code you can easily build an NSArray with all font names and then use it to verify if your font is there, or any kind of workflow is more appropriate to your app.

source

like image 24
sergio Avatar answered Oct 27 '22 04:10

sergio