[RN] ๐Ÿ“expo ํฐํŠธ ๋ณ€๊ฒฝํ•˜๊ธฐ (ft. global-props)

TATAยท2023๋…„ 9์›” 4์ผ
0

React-Native

๋ชฉ๋ก ๋ณด๊ธฐ
5/12

โ–ท react-native expo ํฐํŠธ ๋ณ€๊ฒฝ

โ’ˆ ttfํŒŒ์ผ

๊ตฌ๊ธ€ ํฐํŠธ์—์„œ ์›ํ•˜๋Š” ํฐํŠธ๋ฅผ ๋‹ค์šด๋กœ๋“œ ๋ฐ›์•„ ttfํŒŒ์ผ์„ assets/fonts ํŒŒ์ผ ์•ˆ์— ๋„ฃ์–ด์ค€๋‹ค.


โ’‰ expo-font ์„ค์น˜

# ์„ค์น˜
npm install expo-font

โ’Š ํฐํŠธ ์ ์šฉํ•˜๊ธฐ

ํฐํŠธ ๋กœ๋”ฉ์˜ ๊ธฐ๋ณธ ์‚ฌ์šฉ๋ฒ•: Font.loadAsync({"ํฐํŠธ๋ช…": require("๊ฒฝ๋กœ")});

ํฐํŠธ ๋กœ๋”ฉ์€ ๋น„๋™๊ธฐ ์ž‘์—…์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ Œ๋”๋ง์ด ์‹œ์ž‘๋œ ํ›„์—๋„ ํฐํŠธ ๋กœ๋”ฉ์ด ์™„๋ฃŒ๋˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ useEffect ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํฐํŠธ ๋กœ๋”ฉ์ด ์™„๋ฃŒ๋˜๋ฉด ๋กœ๋”ฉ ์ƒํƒœ๋ฅผ true๋กœ ๋ณ€๊ฒฝํ•ด์ค˜์•ผ ํ•œ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ํฐํŠธ ๋กœ๋”ฉ์ด ์™„๋ฃŒ๋œ ํ›„์—๋งŒ ํ…์ŠคํŠธ ์Šคํƒ€์ผ์— ํฐํŠธ๋ฅผ ์ ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.(์•ˆ ํ•ด์ฃผ๋ฉด ์˜ค๋ฅ˜ ๋œธ)

import * as Font from 'expo-font';
import { useEffect, useState } from 'react';

export default function App() {
  const [fontsLoaded, setFontsLoaded] = useState(false);

  useEffect(() => {
    async function loadFonts() {
      await Font.loadAsync({
        bubblegum: require('./src/assets/fonts/BubblegumSans-Regular.ttf'),
        notosans: require('./src/assets/fonts/NotoSansKR-VariableFont_wght.ttf'),
      });
      setFontsLoaded(true);
    }

    loadFonts();
  }, []);

  if (!fontsLoaded) {
    return null; // ํฐํŠธ ๋กœ๋”ฉ ์ค‘์—๋Š” ๋ Œ๋”๋ง์„ ๋ฐฉ์ง€
  }

  return (
    <SafeAreaView>
      // ์ ์šฉ ๋ฐฉ๋ฒ•
      <Text style={{ fontFamily: 'notosans', fontSize: 28 }}>Font</Text>
      <Text style={{ fontFamily: 'bubblegum', fontSize: 28 }}>Font</Text>
    </SafeAreaView>
  );
}

โ–ท react-native-global-props

notosans ํฐํŠธ๋ฅผ ๋ชจ๋“  Text์— ๊ฐœ๋ณ„์ ์œผ๋กœ ์„ค์ •ํ•˜๋Š” ๋Œ€์‹ , ์ „์—ญ์œผ๋กœ ํ•œ ๋ฒˆ๋งŒ ์„ค์ •ํ•˜์—ฌ ๋ชจ๋“  ํ…์ŠคํŠธ์— ์ผ๊ด„์ ์œผ๋กœ ์ ์šฉํ•˜๊ณ  ์‹ถ์—ˆ๋‹ค.

๊ทธ๋Ÿด ๋• react-native-global-props ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

โ’ˆ ์„ค์น˜

# ์„ค์น˜
npm i react-native-global-props --save
npm install --save-dev @types/react-native-global-props

โ’‰ setCustomText

setCustomText๋กœ ํฐํŠธ๋ฅผ ์ ์šฉ

import { setCustomText } from 'react-native-global-props';

const customTextProps = {
  style: {
    fontFamily: 'notosans',
  },
};

setCustomText(customTextProps);

์ „์ฒด ์ฝ”๋“œ

import * as Font from 'expo-font';
import { useEffect, useState } from 'react';
import { setCustomText } from 'react-native-global-props';

export default function App() {
  const [fontsLoaded, setFontsLoaded] = useState(false);

  useEffect(() => {
    async function loadFonts() {
      await Font.loadAsync({
        bubblegum: require('./src/assets/fonts/BubblegumSans-Regular.ttf'),
        notosans: require('./src/assets/fonts/NotoSansKR-VariableFont_wght.ttf'),
      });
      setFontsLoaded(true);
    }

    loadFonts();
  }, []);

  if (!fontsLoaded) {
    return null; // ํฐํŠธ ๋กœ๋”ฉ ์ค‘์—๋Š” ๋ Œ๋”๋ง์„ ๋ฐฉ์ง€
  }

  const customTextProps = {
    style: {
      fontFamily: 'notosans',
    },
  };
  setCustomText(customTextProps);

  return (
    <SafeAreaView>
      <Text style={{ fontSize: 28 }}>Font</Text> // โญ๏ธfontFamily: 'notosans'๋ฅผ ํ•ด์ฃผ์ง€ ์•Š์•„๋„ ์ ์šฉ๋จ
      <Text style={{ fontFamily: 'bubblegum', fontSize: 28 }}>Font</Text>
    </SafeAreaView>
  );
}
profile
๐ŸŒฟ https://www.tatahyeonv.com

0๊ฐœ์˜ ๋Œ“๊ธ€