Monday, 31 March 2014

Fetch list of fonts and font family in iPhone programmatically using XCode. Steps to add Custom Fonts in xcode. Fetch list of Custom fonts added to app.

Fetch list of Default fonts.

-(voidfetchDefaultFonts
{
    // [UIFont familyNames] returns the array of available fonts family
    for (id familyName in [UIFont familyNames]) {
        NSLog(@"%@", familyName);
        // [UIFont fontNamesForFamilyName:familyName] returns the array of available fonts that fontFamily
        for (id fontName in [UIFont fontNamesForFamilyName: familyName]){
             NSLog(@"%@", fontName);
        }
    }
}

iOS 5 fonts
iOS 6 Fonts



Fetch list of Custom fonts added to app.

NSDictionary* infoDict = [[NSBundle mainBundle] infoDictionary];
NSArray* fontFiles = [infoDict objectForKey:@"UIAppFonts"];

for (NSString *fontFile in fontFiles) {
    NSLog(@"file name: %@", fontFile);
    NSURL *url = [[NSBundle mainBundle] URLForResource:fontFile withExtension:NULL];
    NSData *fontData = [NSData dataWithContentsOfURL:url];
    CGDataProviderRef fontDataProvider = CGDataProviderCreateWithCFData((__bridge CFDataRef)fontData);
    CGFontRef loadedFont = CGFontCreateWithDataProvider(fontDataProvider);
    NSString *fullName = CFBridgingRelease(CGFontCopyFullName(loadedFont));
    CGFontRelease(loadedFont);
    CGDataProviderRelease(fontDataProvider);
    NSLog(@"font name: %@", fullName);
}

Steps to add Custom Fonts
Add by draging your custom font file(Eg: "myFont-Test.ttf") in XCode project.
1.) Open your project "YourApp-info.plist" under "Supporting Files" folder.
2.) Add new row in the list with key as "Fonts provided by application"or"UIAppFonts".
3.) In this array add your fonts with full name. Eg: item 0: "myFont-Test.ttf"
4.) Use in your new font in code as
[UIFont fontWithName:@"myFont-Test" size:16.0];




No comments:

Post a Comment