๊ฐœ๋ฐœ ๐Ÿพ/React Native

[expo] ์›นํฐํŠธ webfont ์ ์šฉํ•˜๊ธฐ

JOTOKKI 2022. 7. 18. 21:45
728x90

์‚ฌ์‹ค expo ๊ณต์‹ ๋ฌธ์„œ์— ๋‹ค ๋‚˜์™€์žˆ๋‹ค. ๊ทผ๋ฐ ๊ณต์‹๋ฌธ์„œ ๋’ค์ง€๊ธฐ๋„ ๊ท€์ฐฎ์œผ๋‹ˆ ์ •๋ฆฌํ•ด ๋‘๊ธฐ๋กœ ํ–ˆ๋‹ค. 

๊ณต์‹ ๋ฌธ์„œ url : https://docs.expo.dev/guides/using-custom-fonts/

 

 

1. ์„ค์น˜

 @expo-google-fonts/inter ๋ฅผ ๋จผ์ € ์„ค์น˜ํ•ด ์ค€๋‹ค. 

$ expo install expo-font @expo-google-fonts/inter

 

2. ์ ์šฉํ•  ํฐํŠธ ์ฐพ๊ธฐ

https://fonts.google.com/ ์‚ฌ์ดํŠธ์— ๋“ค์–ด๊ฐ€์„œ ์›ํ•˜๋Š” ํฐํŠธ๋ฅผ ์ฐพ๊ณ  ๋‹ค์šด๋กœ๋“œํ•œ๋‹ค. ํ˜น์€ ์ด๋ฆ„๋งŒ ์•Œ์•„๋„ ๋˜๋Š” ๋ชจ์–‘์ด๋‹ค. 

๋‚˜๋Š” ๋ˆˆ์— ๋„๊ฒŒ ๋น„๊ตํ•˜๊ธฐ ์œ„ํ•œ ํฐํŠธ๋ฅผ ์ฐพ์•˜๋‹ค. "Edu SA Beginner" ๋ผ๋Š” ๊ธ€์”จ์ฒด๋ฅผ ์ฐพ์•˜๋‹ค.

ํฐํŠธ ์˜ˆ์‹œ

 

3. App์— component๊ฐ€ render๋˜๊ธฐ์ „์— ์ ์šฉํ•˜๊ธฐ 

import { useFonts } from 'expo-font ๋ฅผ ํ†ตํ•ด useFont๋ฅผ ๋ถˆ๋Ÿฌ์˜จ ๋’ค font load๊ฐ€ ๋˜๊ธฐ์ „์—” statusbar๋ฅผ ๋„์›Œ์ฃผ๊ณ , 

๋‹ค ๋ถˆ๋Ÿฌ์™€์กŒ๋‹ค๋ฉด ์ปดํฌ๋„ŒํŠธ๋ฅผ render ํ•œ๋‹ค. 

import React from 'react';
import { Text, View } from 'react-native';
import { StatusBar } from 'expo-status-bar';
import { useFonts } from 'expo-font';
import Home from './screens/Home'

export default props => {
  let [fontsLoaded] = useFonts({
    'Edu-SA-Beginner-regular': require('./assets/fonts/EduSABeginner-Regular.ttf'),
    'Edu-SA-Beginner-medium': require('./assets/fonts/EduSABeginner-Medium.ttf'),
    'Edu-SA-Beginner-bold': require('./assets/fonts/EduSABeginner-Bold.ttf'),
    'Edu-SA-Beginner-semibold': require('./assets/fonts/EduSABeginner-SemiBold.ttf')
  });

  if (!fontsLoaded) {
    return <StatusBar />;
  }

  return (
    <Home/>
  );
};

 

4. component์—์„œ ์จ๋ณด๊ธฐ 

์ด๋ ‡๊ฒŒ ๋ถˆ๋Ÿฌ์˜ค๊ณ ๋‚˜๋ฉด ์ „์—ญ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. 

์‚ฌ์šฉ๋ฒ•์€ ๊ทธ๋ƒฅ ๊ธฐ์กด์ฒ˜๋Ÿผ fontFamily์— ์‚ฌ์šฉํ•˜๋ฉด๋œ๋‹ค. 

// Home.js
import { StyleSheet, View, Text } from 'react-native'
import React, { useState } from 'react'


export default function Home() {
  return (
    <View>
      <Text style={{fontFamily: 'Edu-SA-Beginner-semibold'}}>Home</Text>
      <Text>Home</Text>
    </View>
  )
}

 

์ ์šฉ ์™„๋ฃŒ๋œ ํ™”๋ฉด

์ž˜ ์ ์šฉ๋œ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. ๐Ÿ˜‰

๋ฐ˜์‘ํ˜•