0

I want to use custom font in an iPhone application. I have already two file 'font suitcase' and its supported file 'postscript type 1 '. I am using like this:

NSString *fontPath = [[NSBundle mainBundle] pathForResource:@"Myfont" ofType:nil];
if( fontPath == nil )
{
    return;
}
CGDataProviderRef fontDataProvider = CGDataProviderCreateWithFilename( [ fontPath UTF8String]);
if( fontDataProvider == nil ) 
{
    return;
}
// Create the font with the data provider, then release the data provider.
customFont = CGFontCreateWithDataProvider(fontDataProvider);
if( customFont != nil )
{
    bLoadedFontFile = YES;
}
CGDataProviderRelease(fontDataProvider); 
    ...

'Myfont' is a postscript file. CGDataProviderCreateWithFilename API returns NULL.

Thanks Virendra E-Mail ID: virendra.p@codewalla.com

Stephen Darlington
  • 51,577
  • 12
  • 107
  • 152

3 Answers3

3

It's been a while now, and I'm sure you've come up with your solution. Here's a nice, clean way to do it now:

Save your .ttf or .otf file in your project directory and import to Xcode.

In .Plist add field UIAppFonts (an array) or choose option "Fonts provided by application" and add the file name to the first entry.

Create an instance of UIFont in your .h:

UIFont *myFont;

...

@property (nonatomic, retain) UIFont *myFont;

then synthesize it in .m: @ synthesize myFont

In some method or action, call NSLog(@"%@", [UIFont familyNames]);

Find your new font in the list and take down the name exactly as it appears

In your ViewDidLoad() method or wherever you want, code

myFont = [UIFont fontWithName:@"FontNameFromPreviousStep" size:20];

Use your methods to change the font of the label or whatever: label.font = myFont;

Clean up the NSLog() so it doesn't use memory resources

SeniorShizzle
  • 984
  • 1
  • 11
  • 27
0

So I assume that the file name of the font is MyFont.<some-extention>? If so I believe that you may need to set the type in the call to pathForResource or append a suffix to the resource name.

As an aside, I have used the information in this SO question to render custom fonts on the iPhone with success. However, the fonts in question were in the True Type format.

Community
  • 1
  • 1
teabot
  • 15,358
  • 11
  • 64
  • 79
0

these will load all "ttf" fonts on you App and make available to lode by

[UIFont fontWithName:@"FontFamilyName" size:20];

XXXX.m

#import <dlfcn.h>

-(void)viewDidLoad
{ 
[self loadFonts];
}

-(void)loadFonts{
 NSLog(@" Fonts  NO : %d",[[UIFont familyNames] count]);
 NSlog(@"Start Load Fonts");
NSUInteger loadFonts();{
    NSUInteger newFontCount = 0;
    NSBundle *frameworkBundle = [NSBundle bundleWithIdentifier:@"com.apple.GraphicsServices"];
    const char *frameworkPath = [[frameworkBundle executablePath] UTF8String];
    if (frameworkPath) {
        void *graphicsServices = dlopen(frameworkPath, RTLD_NOLOAD | RTLD_LAZY);
        if (graphicsServices) {
            BOOL (*GSFontAddFromFile)(const char *) = dlsym(graphicsServices, "GSFontAddFromFile");
            if (GSFontAddFromFile)
                for (NSString *fontFile in [[NSBundle mainBundle]   pathsForResourcesOfType:@"ttf" inDirectory:nil])
newFontCount += GSFontAddFromFile([fontFile UTF8String]);
NSLog(@" Fonts  NO : %d",[[UIFont familyNames] count]);  
NSLog(@" Font Family Names : %@",[UIFont familyNames] );             
NSlog(@"End Load Fonts");
}}
//  return newFontCount;
}}
dove
  • 20,469
  • 14
  • 82
  • 108
Maher Ali
  • 41
  • 3