bookmark_borderCustom Font Example iPhone

An example for using custom font in iPhone.

Initially, I have tried with UIFont for using custom font, but it seems not possible.

Documents from apple:
http://developer.apple.com/iphone/library/documentation/GraphicsImaging/Reference/CGFont/Reference/reference.html

Create custom font using CGDataProvider:

NSString *fontPath = [[NSBundle mainBundle] pathForResource:@”CUSTOM_FONT” ofType:@”ttf”];
CGDataProviderRef fontDataProvider = CGDataProviderCreateWithFilename([fontPath UTF8String]); CGFontRef customFont = CGFontCreateWithDataProvider(fontDataProvider);

Use the font:

CGContextSetFont(context, customFont);
CGContextSetFontSize(context, 34.0);
CGContextSetTextDrawingMode(context, kCGTextFill);
CGGlyph textToPrint[[mainString length]];
// Loop through the entire length of the text.
for (int i = 0; i < [mainString length]; ++i) { // Store each letter in a Glyph and subtract the MagicNumber to get appropriate value.
textToPrint[i] = [[mainString uppercaseString] characterAtIndex:i] + 332;
}

//to understand the MagicNumber, I open the font file using FontForge
//found that the font I am using started from location 32
//after I changed the “Encoding->Reencode” to “Glyph Order”
//I found that the “U+0020 space” started from location 3
//