프로젝트에서 사용할 폰트를 설정해보겠다.
구글폰트(google fonts)에서 폰트를 다운로드 받는다.
원하는 폰트가 없으면 구글링해서 폰트 파일을 받아도 무방하다.
폰트 파일 확장자가 다수 존재하는데, 여기에서는 woff 파일을 사용하도록 하겠다.
(폰트 확장자 관련 참고 1)
(폰트 확장자 관련 참고 2)
woff 파일을 사용할 것이기 때문에 ttf 파일을 woff 파일로 변환해야한다.
온라인에서 변환을 지원해주는 사이트(anyconv.com)에서 woff 파일로 변환한다.
/src/Assets/Fonts 경로에 폰트 파일을 추가한다.
// GlobalFont.ts
import { createGlobalStyle } from 'styled-components';
// 각 폰트 파일 import
import Font_L from '../Assets/Fonts/Font-Light.woff';
import Font_R from '../Assets/Fonts/Font-Regular.woff';
import Font_B from '../Assets/Fonts/Font-Bold.woff';
export default createGlobalStyle`
@font-face {
font-family: "Font_test";
src: local("Font_test"), url(${Font_L}) format('woff');
font-weight: lighter;
}
@font-face {
font-family: "Font_test";
src: local("Font_test"), url(${Font_R}) format('woff');
font-weight: normal;
}
@font-face {
font-family: "Font_test";
src: local("Font_test"), url(${Font_B}) format('woff');
font-weight: bold;
}
`;
woff 파일을 typescript가 인식을 하지 못해서 에러가 발생하기 때문이다.
// Types.d.ts
declare module '*.jpg';
declare module '*.png';
declare module '*.jpeg';
declare module '*.gif';
declare module '*.woff';
// App.tsx
import React from 'react';
import { BrowserRouter as Router } from 'react-router-dom';
import RoutesComponent from './Pages/Routes';
import Globalstyle from './Styles/Globalstyle';
import GlobalFont from './Styles/GlobalFont';
function App(): JSX.Element {
return (
<>
<Globalstyle />
<GlobalFont /> // GlobalFont 추가
<Router>
<RoutesComponent />
</Router>
</>
);
}
export default App;
src/Styles/Globalstyle.ts 파일에서 폰트를 추가한다.
// Globalstyles.ts
import { createGlobalStyle } from 'styled-components';
import { reset } from 'styled-reset';
export default createGlobalStyle`
${reset}
*, *::before, *::after{
box-sizing: border-box;
}
html{
font-size: 1vw;
}
body{
font-family: "Font-test"; // 폰트 설정
}
a{
color: inherit;
text-decoration: none;
}
ul{
list-style: none;
}
`;
아래 명령어를 사용하여 개발 서버를 실행시켜 폰트 적용 여부를 확인한다.
npm start
적용 완료!