From cc83c514ab1f014219982e65834dfec033d23096 Mon Sep 17 00:00:00 2001 From: Nuno Campos Date: Sat, 6 May 2017 16:16:38 +0100 Subject: [PATCH] Support usage with cocoapods with `use_frameworks!` - Explicitly load font files from the module bundle without assuming they're present in the main app bundle --- RNVectorIconsManager/RNVectorIconsManager.m | 35 ++++++++++++++++++++- lib/create-icon-set.js | 6 ++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/RNVectorIconsManager/RNVectorIconsManager.m b/RNVectorIconsManager/RNVectorIconsManager.m index fb76702..46622ad 100644 --- a/RNVectorIconsManager/RNVectorIconsManager.m +++ b/RNVectorIconsManager/RNVectorIconsManager.m @@ -7,6 +7,7 @@ // #import "RNVectorIconsManager.h" +#import #if __has_include() #import #else // Compatibility for RN version < 0.40 @@ -70,6 +71,38 @@ RCT_EXPORT_METHOD(getImageForFont:(NSString*)fontName withGlyph:(NSString*)glyph } } callback(@[[NSNull null], filePath]); - } + +RCT_EXPORT_METHOD(loadFontWithFileName:(NSString *)fontFileName + extension:(NSString *)extension + resolver:(RCTPromiseResolveBlock)resolve + rejecter:(RCTPromiseRejectBlock)reject) +{ + NSBundle *bundle = [NSBundle bundleForClass:[self class]]; + NSURL *fontURL = [bundle URLForResource:fontFileName withExtension:extension]; + NSData *fontData = [NSData dataWithContentsOfURL:fontURL]; + + CGDataProviderRef provider = CGDataProviderCreateWithCFData((CFDataRef)fontData); + CGFontRef font = CGFontCreateWithDataProvider(provider); + + if (font) { + CFErrorRef errorRef = NULL; + if (CTFontManagerRegisterGraphicsFont(font, &errorRef) == NO) { + NSError *error = (__bridge NSError *)errorRef; + if (error.code == kCTFontManagerErrorAlreadyRegistered) { + resolve(nil); + } else { + reject(@"font_load_failed", @"Font failed to load", error); + } + } else { + resolve(nil); + } + + CFRelease(font); + } + if (provider) { + CFRelease(provider); + } +} + @end diff --git a/lib/create-icon-set.js b/lib/create-icon-set.js index 818307c..d0f27f4 100644 --- a/lib/create-icon-set.js +++ b/lib/create-icon-set.js @@ -29,6 +29,12 @@ export default function createIconSet(glyphMap, fontFamily, fontFile) { fontReference = `Assets/${fontFile}#${fontFamily}`; } + if (Platform.OS === 'ios' && fontFile) { + NativeIconAPI.loadFontWithFileName(...fontFile.split('.')).catch(error => + console.error(error) + ); + } + const IconNamePropType = PropTypes.oneOf(Object.keys(glyphMap)); class Icon extends Component {