React Native中如何导入可变字体文件的全部字重与样式
解决Expo中可变字体的多字重调用问题
你已经正确导入了可变字体文件,不需要逐个导入静态字体文件,只需通过fontWeight属性指定具体数值,就能调用可变字体包含的所有字重。
具体操作方法:
- 保留你当前的字体导入代码(已正确加载可变字体文件)
- 在
Text组件的样式中,通过fontWeight设置对应字重的数值(需匹配所用可变字体支持的字重范围,比如Quicksand可变字体的字重范围是300-700)
修改后的代码示例:
import { StyleSheet, Text, View } from 'react-native'; import { useFonts } from "expo-font"; import * as SplashScreen from 'expo-splash-screen'; import { useCallback } from 'react'; SplashScreen.preventAutoHideAsync(); export default function App() { const [fontsLoaded, fontError] = useFonts({ "Quicksand" : require('./assets/fonts/Quicksand/Quicksand-VariableFont_wght.ttf') }); const onLayoutRootView = useCallback(async () => { if (fontsLoaded || fontError) { await SplashScreen.hideAsync(); } }, [fontsLoaded, fontError]); if (!fontsLoaded && !fontError) { return null; } return ( <View style={styles.container} onLayout={onLayoutRootView}> <Text style={[styles.baseText, {fontWeight: '300'}]}>Quicksand Light (300)</Text> <Text style={[styles.baseText, {fontWeight: '400'}]}>Quicksand Regular (400)</Text> <Text style={[styles.baseText, {fontWeight: '600'}]}>Quicksand SemiBold (600)</Text> <Text style={[styles.baseText, {fontWeight: '700'}]}>Quicksand Bold (700)</Text> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#fff', alignItems: 'center', justifyContent: 'center', gap: 10 }, baseText: { fontFamily: "Quicksand", fontSize: 30, } });
注意事项:
- 确认你的可变字体文件确实支持对应字重范围(可通过字体查看工具验证,比如Font Book)
- 确保
expo-font版本在~11.0.0及以上,旧版本对可变字体的支持有限 - 优先使用数值字符串(如
'300')而非'light'这类描述性字符串,避免兼容性问题
内容的提问来源于stack exchange,提问作者ramalandajo
相关产品推荐
相关产品推荐

