Ich dachte, ich meine Antwort teilen würde als Gut. Mein Projekt ist wie so ein:
Haupt iOS App (Swift)
Dynamische Framework (Obj-C)
App Classes (Viewcontrollers, Models, Coredata, etc ...)
Mein Ziel war es zu sein Die Hauptanwendung kann eine einzige Methode auf dem dynamischen Framework aufrufen, um Zeichensätze zu laden, ohne dass die Datei Info.plist geändert oder die Schriftartdateien hinzugefügt werden müssen. Bündel zum Hauptziel.
@import CoreText;
@implementation NSBundle (Fonts)
+ (NSBundle *)fontsBundle {
// The only way I could find to do this is to hard code the sub-path. Using pathForResource doesn't seem to find Fonts.bundle, nor its contents\
// This way the host app doesn't need to copy Fonts.bundle
NSString *path = [[[NSBundle mainBundle] bundlePath] stringByAppendingString:@"/Frameworks/<YourFrameworkName>.framework/Fonts.bundle"];
NSBundle *bundle = [NSBundle bundleWithPath:path];
if (bundle == nil) {
NSLog(@"Warning: Fonts.bundle could not be loaded. Have you included it in your target?");
}
return bundle;
}
- (BOOL)loadFonts {
NSArray<NSString *> *names = @[
@"GothamRnd-Bold",
@"GothamRnd-BoldItal",
@"GothamRnd-Book",
@"GothamRnd-BookItal",
@"GothamRnd-Light",
@"GothamRnd-LightItal",
@"GothamRnd-MedItal",
@"GothamRnd-Medium",
];
__block NSInteger failCounter = 0;
[names enumerateObjectsUsingBlock:^(id _Nonnull name, NSUInteger idx, BOOL *_Nonnull stop) {
NSString *fontPath = [self pathForResource:name ofType:@"otf"];
NSData *inData = [NSData dataWithContentsOfFile:fontPath];
CFErrorRef error;
CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)inData);
CGFontRef font = CGFontCreateWithDataProvider(provider);
if (!CTFontManagerRegisterGraphicsFont(font, &error)) {
if (error) {
NSLog(@"Failed to load font at path: %@", fontPath);
failCounter++;
}
CFStringRef errorDescription = CFErrorCopyDescription(error);
NSLog(@"Failed to load font: %@", errorDescription);
CFRelease(errorDescription);
}
CFRelease(font);
CFRelease(provider);
}];
return failCounter == 0;
}
@end
Der einzige Nachteil in diesem Code ist, Sie müssen den Pfad zu den Fonts.bundle hart Code. Ich konnte keine Kombination von NSBundle-Methoden erhalten, um die Fonts.bundle-Datei automatisch zu finden.Zum Beispiel, wie dies keine Verfahren würde einen Weg zurück:
NSString *pathToBundle = [[NSBundle mainBundle] pathForResource:@"Fonts" ofType:@"bundle"];
NSString *pathToFont = [[NSBundle mainBundle] pathForResource:@"MyFont" ofType:@"ttf"];
Abgesehen von der harten Codierung (das wird sich nie ändern), ist dies für mich zu arbeiten, obwohl gut genug. Ich kann jetzt alle meine Client-Apps problemlos aufbereiten.
Haben Sie die Methode in meiner Antwort getestet? Ich verwende es in einigen Apps, die benutzerdefinierte Schriftarten verwenden, die mit einem dynamischen Framework gebündelt sind, ohne dass die Schriftarten zum Hauptprojekt hinzugefügt werden müssen. –