How to load only Custom font names in Array?

716 views Asked by At

I am using Multiple custom fonts in my app.i know how to Add custom font but the Terrible parts of all this process is when we are going to use the custom font because mostly the custom font name is different from the file that we are using in app.here i would like to explain the recent example that i did,First of all i have added two custom font file in my app enter image description here

For getting the above custom font names i tried this way

NSArray *familyNames = [[NSArray alloc] initWithArray:[UIFont familyNames]];

NSArray *fontNames;
NSInteger indFamily, indFont;
 FontnameArray=[[NSMutableArray alloc] init];
for (indFamily=0; indFamily<[familyNames count]; ++indFamily)
{
   fontNames = [[NSArray alloc] initWithArray:
                 [UIFont fontNamesForFamilyName:
                  [familyNames objectAtIndex:indFamily]]];
    for (indFont=0; indFont<[fontNames count]; ++indFont)
    {
        NSString *fullName = [NSString stringWithFormat:@"%@",[fontNames objectAtIndex:indFont]];
        [FontnameArray addObject:fullName];
        }
    }
[FontnameArray sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
NSLog(@"    Font name: %@", FontnameArray);

The above Code display 199 font names for me ,so it was to difficult for me to pick the custom font name among 199 fonts, To make my doubt clear then i have simply drag the font file to font book Then i got the orignal names of custom Font.

[UIFont fontWithName:@"Ethiopia Primary" size:20]];
[UIFont fontWithName:@"Ethiopia jiret" size:20]];

So the Question is how can i get all Custom font names in Array like Above instead of draging the each font file to Fontbook for getting orignal names.

2

There are 2 answers

0
LE SANG On

Just compare with all fonts exist in iOS

*Create new project like this code and write array of all font names to a plist file

- (void)writeAlliOSFontsToPlist{
    NSArray *fontFamilies = [UIFont familyNames];
    [self savePlist:@"AllFont" fromArray:fontFamilies];
}

- (NSString *) getFilePathForPlist:(NSString *) plistName {
    // placeholder
    NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    return [[pathArray objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist",plistName]];
}

-(void) savePlist:(NSString *)plistName fromArray:(NSArray *)array {
    [array writeToFile:[self getFilePathForPlist:plistName] atomically:YES];
}

Open your simulator dir and copy this plist to your App bundle. Then read this plist to an array and compare with all of your fonts by this code

-(void)dumpCustomFonts{
    NSArray *iOSFontsArray = [NSArray arrayWithContentsOfFile:
                              [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:
                               @"AllFont.plist"]];
    NSMutableArray *fontFamilies = (NSMutableArray *)[UIFont familyNames];
    [fontFamilies removeObjectsInArray:iOSFontsArray];
    NSLog(@"%@",fontFamilies);
}

I've done all in my test project, you just copy and paste it.

3
Maulik On

You should add your custom font in .plist by

1) Add a new entry with the key "Fonts provided by application".

2) Now add each custom font file name with extension to this array like :

enter image description here

Now you can retrive all your custom fonts by :

NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:@"Info.plist"];
NSDictionary *plistData = [[NSDictionary dictionaryWithContentsOfFile:finalPath] retain];

fontFamilyNames = [plistData valueForKey:@"UIAppFonts"];
NSLog(@"%@",fontFamilyNames);

NSString *fontFamilyName = [[fontFamilyNames objectAtIndex:index] stringByDeletingPathExtension];
UILabel *fontNameLabel = [[[UILabel alloc] init] autorelease];        
fontNameLabel.text = @"Preview Text";
fontNameLabel.font = [UIFont fontWithName:fontFamilyName size:20.0];