ho pensato di condividere la mia risposta come bene. Il mio progetto è impostato in questo modo:
il mio obiettivo era di essere in grado di fare in modo che l'app principale chiami un singolo metodo sul framework dinamico per caricare i font senza dover modificare Info.plist o aggiungere i file di font/fascio al bersaglio principale.
@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
L'unico inconveniente in questo codice è che è necessario codificare il percorso del file Fonts.bundle. Non è stato possibile ottenere alcuna combinazione di metodi NSBundle per individuare automaticamente il file Fonts.bundle.Per esempio nessun metodo come questo sarebbe restituire un percorso:
NSString *pathToBundle = [[NSBundle mainBundle] pathForResource:@"Fonts" ofType:@"bundle"];
NSString *pathToFont = [[NSBundle mainBundle] pathForResource:@"MyFont" ofType:@"ttf"];
A parte la codifica (che non cambierà mai) del disco, questo sta lavorando per me abbastanza bene però. Ora posso applicare facilmente tutte le mie app client.
Hai provato il metodo nella mia risposta? L'ho usato in alcune app che utilizzano font personalizzati in bundle con un framework dinamico, senza la necessità di aggiungere i font al progetto principale. –