mirror of
https://github.com/zhigang1992/react-native-vector-icons.git
synced 2026-01-12 17:42:58 +08:00
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:
committed by
Joel Arvidsson
parent
7f73df5731
commit
cc83c514ab
@@ -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
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user