๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ“ฑ Cross Platform/React Native

[React Native] Text์— Custom Font ์ ์šฉํ•˜๊ธฐ

by Fomagran ๐Ÿ’ป 2022. 6. 17.
728x90
๋ฐ˜์‘ํ˜•

์•ˆ๋…•ํ•˜์„ธ์š” Foma ์ž…๋‹ˆ๋‹ค!

 

์˜ค๋Š˜์€ React Native์—์„œ Custom Font๋ฅผ ๋‹ค์šด ๋ฐ›์•„ ์ ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ์•Œ์•„ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

๋ฐ”๋กœ ์‹œ์ž‘ํ• ๊ฒŒ์š”~

 

์–ธ์–ด๋Š” TypeScript, ํ™˜๊ฒฝ์€ Expo๋กœ ์ง„ํ–‰ ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค!


Download Font

 

์•„๋ž˜ Google Font๋กœ ์ด๋™ํ•ด ์ค๋‹ˆ๋‹ค.

 

 

Google Fonts

Making the web more beautiful, fast, and open through great typography

fonts.google.com

 

์›ํ•˜๋Š” ํฐํŠธ๋ฅผ ๊ณ ๋ฅธ ๋’ค

 

 

Download family ๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ ๋ง˜์— ๋“œ๋Š” ํฐํŠธ๋ฅผ ๋‹ค์šด๋กœ๋“œ ๋ฐ›์•„ ์ค๋‹ˆ๋‹ค.

 

 

.zipํŒŒ์ผ์„ ์••์ถ• ํ•ด์ œํ•œ ๋’ค assets์— fonts ํด๋”๋ฅผ ๋งŒ๋“ค์–ด ์ฃผ๊ณ  ํ•ด๋‹น .ttf ํŒŒ์ผ์„ ๋„ฃ์–ด์ค๋‹ˆ๋‹ค.

 


Use Cached Resources

 

useCachedResources.ts

 

ํฐํŠธ๋ฅผ ์—ฌ๋Ÿฌ๋ฒˆ ๋ถˆ๋Ÿฌ์˜ค๋Š” ๊ฒƒ์„ ๋ง‰๊ธฐ ์œ„ํ•ด useState์™€ useEffect๋ฅผ ์‚ฌ์šฉํ•ด ๋”ฑ ํ•œ๋ฒˆ๋งŒ ๋ถˆ๋Ÿฌ์˜ค๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.

 

useEffect ์•ˆ์— ๋น„๋™๊ธฐ์ ์œผ๋กœ ์œ„์—์„œ ๋‹ค์šด ๋ฐ›์€ ํฐํŠธ๋ฅผ ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์žˆ๋„๋ก loadResourceAndDataAsync ๋ฉ”์„œ๋“œ๋ฅผ ์ž‘์„ฑํ•ด ์ค๋‹ˆ๋‹ค.

 

ํฐํŠธ๋ฅผ ๋‹ค ๋ถˆ๋Ÿฌ ์™”๋‹ค๋ฉด setIsLoadingComplete๋ฅผ true๋กœ ํ•ด์ค˜ isLoadingComplete๊ฐ€ true๋กœ ๋ฐ˜ํ™˜๋  ์ˆ˜ ์žˆ๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.

 

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

export default function useCachedResources() {
  const [isLoadingComplete, setIsLoadingComplete] = useState(false);

  useEffect(() => {
    async function loadResourcesAndDataAsync() {
      try {
        await Font.loadAsync({
          "fascinate-regular": require("../assets/fonts/Fascinate-Regular.ttf"),
        });
      } catch (e) {
        console.warn(e);
      } finally {
        setIsLoadingComplete(true);
      }
    }

    loadResourcesAndDataAsync();
  }, [isLoadingComplete]);

  return isLoadingComplete;
}

Adjust Font

 

์œ„์—์„œ ๋งŒ๋“  useCachedResources๋ฅผ ํ†ตํ•ด ํฐํŠธ๊ฐ€ ๋ถˆ๋ ค์กŒ๋‹ค๋ฉด View๋ฅผ returnํ•˜๊ณ  ๊ทธ๋ ‡์ง€ ์•Š๋‹ค๋ฉด null์„ ๋ฐ˜ํ™˜ํ•˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.

 

App.tsx

 

import { StyleSheet, Text, View } from "react-native";
import useCachedResources from "./hooks/useCachedResources";

export default function App() {
  const isLoaded = useCachedResources();

  if (isLoaded) {
    return (
      <View style={styles.container}>
        <Text style={styles.text}>Hello, I'm Fomagran</Text>
      </View>
    );
  } else {
    return null;
  }
}

 

๊ทธ ๋‹ค์Œ styles์— fontFamily๋ฅผ ํ•ด๋‹น ํฐํŠธ ์ด๋ฆ„์œผ๋กœ ๋ฐ”๊ฟ”์ค๋‹ˆ๋‹ค.

 

const styles = StyleSheet.create({
  container: {
    padding: 20,
    flex: 1,
    backgroundColor: "rgba(50,50,50,1)",
  },
  text: {
    fontSize: 20,
    marginBottom: 20,
    fontWeight: "bold",
    color: "white",
    fontFamily: "fascinate-regular",
  },
});

์‹คํ–‰ ๊ฒฐ๊ณผ

 

์•„๋ž˜์™€ ๊ฐ™์ด ํฐํŠธ๊ฐ€ ์ž˜ ์ ์šฉ๋˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

(์™ผ์ชฝ์ด ์ ์šฉํ•˜๊ธฐ ์ „์ด๊ณ  ์˜ค๋ฅธ์ชฝ์ด ํฐํŠธ๋ฅผ ์ ์šฉํ•œ ํ›„์˜ ๋ชจ์Šต์ž…๋‹ˆ๋‹ค.)

 

 

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€