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
This commit is contained in:
Nuno Campos
2017-05-06 16:16:38 +01:00
committed by Joel Arvidsson
parent 7f73df5731
commit cc83c514ab
2 changed files with 40 additions and 1 deletions

View File

@@ -7,6 +7,7 @@
//
#import "RNVectorIconsManager.h"
#import <CoreText/CoreText.h>
#if __has_include(<React/RCTConvert.h>)
#import <React/RCTConvert.h>
#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

View File

@@ -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 {