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