简体   繁体   中英

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?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM