Load custom font in iPhone

Below is the code to load truetype font and use it in resource, so that we can use fonts other than the font built-in in iPhone. Note that make sure that you studied the license of the font that allow you to bundle with application, that most commerical fonts won't allow this. Also most file size of true type font are large.

(void)loadFont{ // Get the path to our custom font and create a data provider.
NSString *fontPath = [[NSBundle mainBundle] pathForResource:@"mycustomfont" ofType:@"ttf"];
CGDataProviderRef fontDataProvider = CGDataProviderCreateWithFilename([fontPath UTF8String]);
// Create the font with the data provider, then release the data provider. customFont =
CGFontCreateWithDataProvider(fontDataProvider); CGDataProviderRelease(fontDataProvider);
}

Now, in your drawRect, do something like this:

-(void)drawRect:(CGRect)rect{
[super drawRect:rect]; // Get the context.
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClearRect(context, rect); // Set the customFont to be the font used to draw.
CGContextSetFont(context, customFont);
// Set how the context draws the font, what color, how big.
CGContextSetTextDrawingMode(context, kCGTextFillStroke); CGContextSetFillColorWithColor(context, self.fontColor.CGColor); UIColor * strokeColor = [UIColor blackColor];
CGContextSetStrokeColorWithColor(context, strokeColor.CGColor); CGContextSetFontSize(context, 48.0f);
// Create an array of Glyph's the size of text that will be drawn.
CGGlyph textToPrint[[self.theText length]];
// Loop through the entire length of the text.
for (int i = 0; i < [self.theText length]; ++i) { // Store each letter in a Glyph and subtract the MagicNumber to get appropriate value.
textToPrint[i] = [[self.theText uppercaseString] characterAtIndex:i] + 3 - 32;
}
CGAffineTransform textTransform = CGAffineTransformMake(1.0, 0.0, 0.0, -1.0, 0.0, 0.0);
CGContextSetTextMatrix(context, textTransform);
CGContextShowGlyphsAtPoint(context, 20, 50, textToPrint, [self.theText length]);
}

Comments :

some fonts work

Hmm, I can get this to work for fonts copied over from /Library/Fonts into my project's Resources directory (both dfont and ttf) but I can't get it to work for ttf fonts downloaded from the net. I must be missing something dumb and fundamental but not sure what.

Re: Some fonts work

Hi,
Me too cant get the code work for fonts Downloaded from internet. It is not difficult to use fonts already in iphone. Below single line of code will do the trick.

Eg: CGContextSelectFont (context,"Zapfino",30, kCGEncodingMacRoman);

Water ,
Any help as I am trying to use asian fonts?

No for this trick, since it

No for this trick, since it seems didn't use fallback font, so if your font only contains asian font, other western font will be shown as square. After OS 3.2, there is a new API that you can use font in resource, that will be a lot easier. But you still cannot load fonts from internet and dynamically use them.

Can you provide a link to the

Can you provide a link to the new API in OS3.2?

Thanks

Thanks

a question !

we need iphone sdk for that ?

Sure you need iPhone SDK

Sure you need iPhone SDK