@@ -456,55 +456,100 @@ - (void)setFontIfPresent:(NSString *)fontPath forTheme:(IBGTheme *)theme type:(N
456
456
_registeredFonts = [NSMutableSet set ];
457
457
}
458
458
459
- UIFont *font = [UIFont fontWithName: fontPath size: UIFont.systemFontSize];
460
-
461
- if (!font && ![_registeredFonts containsObject: fontPath]) {
462
- NSString *fontFileName = [fontPath stringByDeletingPathExtension ];
463
- NSArray *fontExtensions = @[@" ttf" , @" otf" , @" woff" , @" woff2" ];
464
- NSString *fontFilePath = nil ;
465
-
466
- for (NSString *extension in fontExtensions) {
467
- fontFilePath = [[NSBundle mainBundle ] pathForResource: fontFileName ofType: extension];
468
- if (fontFilePath) break ;
459
+ // Check if font is already registered
460
+ if ([_registeredFonts containsObject: fontPath]) {
461
+ UIFont *font = [UIFont fontWithName: fontPath size: UIFont.systemFontSize];
462
+ if (font) {
463
+ [self setFont: font forTheme: theme type: type];
469
464
}
465
+ return ;
466
+ }
470
467
471
- if (fontFilePath) {
472
- NSData *fontData = [NSData dataWithContentsOfFile: fontFilePath];
473
- if (fontData) {
474
- CGDataProviderRef provider = CGDataProviderCreateWithCFData ((__bridge CFDataRef)fontData);
475
- if (provider) {
476
- CGFontRef cgFont = CGFontCreateWithDataProvider (provider);
477
- if (cgFont) {
478
- CFErrorRef error = NULL ;
479
- if (CTFontManagerRegisterGraphicsFont (cgFont, &error)) {
480
- NSString *postScriptName = (__bridge_transfer NSString *)CGFontCopyPostScriptName (cgFont);
481
- if (postScriptName) {
482
- font = [UIFont fontWithName: postScriptName size: UIFont.systemFontSize];
483
- [_registeredFonts addObject: fontPath];
484
- }
485
- } else if (error) {
486
- CFStringRef desc = CFErrorCopyDescription (error);
487
- CFRelease (desc);
488
- CFRelease (error);
489
- }
490
- CGFontRelease (cgFont);
491
- }
492
- CGDataProviderRelease (provider);
493
- }
494
- }
495
- }
496
- } else if (!font && [_registeredFonts containsObject: fontPath]) {
497
- font = [UIFont fontWithName: fontPath size: UIFont.systemFontSize];
468
+ // Try to load font from system fonts first
469
+ UIFont *font = [UIFont fontWithName: fontPath size: UIFont.systemFontSize];
470
+ if (font) {
471
+ [_registeredFonts addObject: fontPath];
472
+ [self setFont: font forTheme: theme type: type];
473
+ return ;
498
474
}
499
475
476
+ // Try to load font from bundle
477
+ font = [self loadFontFromPath: fontPath];
500
478
if (font) {
501
- if ([type isEqualToString: @" primary" ]) {
502
- theme.primaryTextFont = font;
503
- } else if ([type isEqualToString: @" secondary" ]) {
504
- theme.secondaryTextFont = font;
505
- } else if ([type isEqualToString: @" cta" ]) {
506
- theme.callToActionTextFont = font;
479
+ [_registeredFonts addObject: fontPath];
480
+ [self setFont: font forTheme: theme type: type];
481
+ }
482
+ }
483
+
484
+ - (UIFont *)loadFontFromPath : (NSString *)fontPath {
485
+ NSString *fontFileName = [fontPath stringByDeletingPathExtension ];
486
+ NSArray *fontExtensions = @[@" ttf" , @" otf" , @" woff" , @" woff2" ];
487
+
488
+ // Find font file in bundle
489
+ NSString *fontFilePath = nil ;
490
+ for (NSString *extension in fontExtensions) {
491
+ fontFilePath = [[NSBundle mainBundle ] pathForResource: fontFileName ofType: extension];
492
+ if (fontFilePath) break ;
493
+ }
494
+
495
+ if (!fontFilePath) {
496
+ return nil ;
497
+ }
498
+
499
+ // Load font data
500
+ NSData *fontData = [NSData dataWithContentsOfFile: fontFilePath];
501
+ if (!fontData) {
502
+ return nil ;
503
+ }
504
+
505
+ // Create data provider
506
+ CGDataProviderRef provider = CGDataProviderCreateWithCFData ((__bridge CFDataRef)fontData);
507
+ if (!provider) {
508
+ return nil ;
509
+ }
510
+
511
+ // Create CG font
512
+ CGFontRef cgFont = CGFontCreateWithDataProvider (provider);
513
+ CGDataProviderRelease (provider);
514
+
515
+ if (!cgFont) {
516
+ return nil ;
517
+ }
518
+
519
+ // Register font
520
+ CFErrorRef error = NULL ;
521
+ BOOL registered = CTFontManagerRegisterGraphicsFont (cgFont, &error);
522
+
523
+ if (!registered) {
524
+ if (error) {
525
+ CFStringRef description = CFErrorCopyDescription (error);
526
+ CFRelease (description);
527
+ CFRelease (error);
507
528
}
529
+ CGFontRelease (cgFont);
530
+ return nil ;
531
+ }
532
+
533
+ // Get PostScript name and create UIFont
534
+ NSString *postScriptName = (__bridge_transfer NSString *)CGFontCopyPostScriptName (cgFont);
535
+ CGFontRelease (cgFont);
536
+
537
+ if (!postScriptName) {
538
+ return nil ;
539
+ }
540
+
541
+ return [UIFont fontWithName: postScriptName size: UIFont.systemFontSize];
542
+ }
543
+
544
+ - (void )setFont : (UIFont *)font forTheme : (IBGTheme *)theme type : (NSString *)type {
545
+ if (!font || !theme || !type) return ;
546
+
547
+ if ([type isEqualToString: @" primary" ]) {
548
+ theme.primaryTextFont = font;
549
+ } else if ([type isEqualToString: @" secondary" ]) {
550
+ theme.secondaryTextFont = font;
551
+ } else if ([type isEqualToString: @" cta" ]) {
552
+ theme.callToActionTextFont = font;
508
553
}
509
554
}
510
555
0 commit comments