React Native如何仅链接字体而不触发全库链接?
Hey there! I get the confusion—react-native-maps warning you off react-native link but everyone telling you to use it for fonts. Let’s break down the correct ways to handle this:
1. Use react-native link to Target Only Your Fonts
Good news: the syntax you’re thinking of actually works! You can specify exactly which font files or directories you want to link, and it won’t touch other libraries like react-native-maps. Here’s how:
- For a single font file:
react-native link ./assets/fonts/YourCustomFont-Regular.ttf - For an entire directory of fonts:
react-native link ./assets/fonts
Just note that this works reliably in most RN versions, and it only processes the resources you specify—no unintended linking of other libraries.
2. Manual Configuration (Most Reliable Option)
If you want to avoid react-native link entirely (to play it super safe), manual setup is straightforward and gives you full control:
For iOS:
- Drag your font files (
.ttf/.otf) into your Xcode project, making sure to check "Copy items if needed" and select your app target. - Open your
Info.plistfile and add theFonts provided by applicationkey as an array, listing each font filename:<key>UIAppFonts</key> <array> <string>YourCustomFont-Regular.ttf</string> <string>YourCustomFont-Bold.ttf</string> </array> - Rebuild your iOS project, and you can use the fonts in RN with
fontFamily: 'YourCustomFont-Regular'.
For Android:
- Create a
fontsfolder insideandroid/app/src/main/assets(if it doesn’t exist already). - Copy all your font files into this
fontsdirectory. - Use the fonts directly in RN with
fontFamily: 'YourCustomFont-Regular'(no file extension needed here).
3. Autolinking Note (RN 0.60+)
If you’re on React Native 0.60 or newer, autolinking is enabled by default—but it only handles native dependencies from third-party libraries, not local font assets. So you’ll still need to use either the targeted react-native link command above or manual setup to get your fonts working.
And a quick clarification: the react-native-maps warning about not using react-native link only applies to linking the maps library itself (since it supports autolinking now). Using react-native link for your fonts is totally safe and won’t interfere with maps.
内容的提问来源于stack exchange,提问作者E Pierre

