Thursday, April 26, 2012

Custom font in IPhone Application

Sometimes we have apply our own custom fonts in application.
Normally we add the fontfile name with extension to resources and a entry in plist.
In code we will use the font some thing like this

[lblTest setFont: [UIFont fontWithName: @"fontfilenamewithoutextension" size:30]];

But this doesn't works if font name is different from font file name.
To get the font name use below code.

   NSArray *familyNames = [UIFont familyNames];
    for( NSString *familyName in familyNames ){
        printf( "Family: %s \n", [familyName UTF8String] );
        NSArray *fontNames = [UIFont fontNamesForFamilyName:familyName];
        for( NSString *fontName in fontNames ){
            printf( "\tFont: %s \n", [fontName UTF8String] );
        }
    }

Pick the font name from the list which is printed in the log and then just replace that 
[lblTest setFont: [UIFont fontWithName@"fontname" size:30]];


Now your custom font works :).

No comments: