tags:

views:

455

answers:

2

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: [email protected]

A: 

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.

teabot
My custom file has no extension. My font type is 'font suitcase' which has no extension.thanksVirendra
A: 

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