Dynamic custom font loader in iOS

I already know how to load a custom font into my project in an iPhone application from here. I want to ask, is there a way to do this from the code? My problem is that I have a resource folder in my application, I have a font file name, let's call it "myfont.ttf".

I want to capture the ttf file and put it in the plist file from the code , and what else do I want to know the display name for fontWithName: size: method. Is there any way to achieve this?

+3
source share
3 answers

Yes, you can. But you have to work a lot with CoreText and / or CoreGraphics.

A nice class from Zynga that can help you with this: https://github.com/zynga/FontLabel

A sample project shows how to load .ttf files from a package without using .plist and use these fonts inside the application.

The code is valid and is a good point from the start.

Edit: The previous approach uses CoreGraphics, which is good, but using Core Text is much better. I found an interesting answer to this question: How can you load a font (TTF) from a file using Core Text?

If you have no experience with the CoreText database, read the official introduction to Apple's documentation .

+4
source

This is an older question, but here is a way to do it anyway if someone else meets this.

+ (void)loadFontAtPath:(NSString*)path{ NSData *data = [[NSFileManager defaultManager] contentsAtPath:path]; if(data == nil){ NSLog(@"Failed to load font. Data at path is null"); return; } CFErrorRef error; CGDataProviderRef provider = CGDataProviderCreateWithCFData((CFDataRef)data); CGFontRef font = CGFontCreateWithDataProvider(provider); if(!CTFontManagerRegisterGraphicsFont(font, &error)){ CFStringRef errorDescription = CFErrorCopyDescription(error); NSLog(@"Failed to load font: %@", errorDescription); CFRelease(errorDescription); } CFRelease(font); CFRelease(provider); } 

This will load the font at the path specified at runtime, then you can use it the same way as usual without adding it to the plist.

+14
source

IF you download a TTF file, then you can do the following to register your custom fonts using the iOS font manager, this code snippet also takes care of the TTF file updates (font updates):

  +(void)registerFontsAtPath:(NSString *)ttfFilePath { NSFileManager * fileManager = [NSFileManager defaultManager]; if ([fileManager fileExistsAtPath:ttfFilePath] == YES) { [UIFont familyNames];//This is here for a bug where font registration API hangs for forever. //In case of TTF file update : Fonts are already registered, first de-register them from Font Manager CFErrorRef cfDe_RegisterError; bool fontsDeregistered = CTFontManagerUnregisterFontsForURL((__bridge CFURLRef)[NSURL fileURLWithPath:ttfFilePath], kCTFontManagerScopeNone, &cfDe_RegisterError); //finally register the fonts with Font Manager, CFErrorRef cfRegisterError; bool fontsRegistered= CTFontManagerRegisterFontsForURL((__bridge CFURLRef)[NSURL fileURLWithPath:ttfFilePath], kCTFontManagerScopeNone, &cfRegisterError); } } 

You can check for logical errors and errors for registration and deregistration.

+1
source

Source: https://habr.com/ru/post/1478938/


All Articles