Search code examples
ioscore-textuifont

Loading a Custom Font to iOS dynamically


Possible Duplicate:
iPhone - Convert CTFont to UIFont?

Do you know how I might be able to add Custom fonts to iOS dynamically without adding it to the project before compile time?

I'm looking at a scenario where the fonts are downloaded during run-time of the app, and then loaded dynamically for usage.

If this is possible, I'd like to use it as a UIFont after loading it.

I've searched through a few possibilities to do this, and found that I can load a font using CoreText:

- (CTFontRef)newCustomFontWithFileName:(NSString *)fontFileName
                                ofType:(NSString *)type
                               andSize:(float)pointSize
{
    NSString *fontPath = [[NSBundle mainBundle] pathForResource:fontFileName ofType:type];
    NSData *data = [[NSData alloc] initWithContentsOfFile:fontPath];
    CGDataProviderRef fontProvider = CGDataProviderCreateWithCFData((CFDataRef)data);
    [data release];

    CGFontRef cgFont = CGFontCreateWithDataProvider(fontProvider);
    CGDataProviderRelease(fontProvider);

    CTFontRef font = CTFontCreateWithGraphicsFont(cgFont, pointSize, NULL, NULL);
    CGFontRelease(cgFont);
    return font;
}

However, after doing this, is it possible to convert CTFontRef to a UIFont for usage throughout the app?

Do let me know what you guys know/think ! :)


Solution

  • As of iOS 5.1, there is no public API that creates a UIFont from a CGFont or a CTFont. You are out of luck. :(

    Open a feature request at http://bugreport.apple.com if you want them to add an API for this.