我正在尝试在利用 Emotion 及其Global component的 React 项目中使用本地托管的字体。此方法适用于网络字体,例如Google Fonts,但是当我下载相同的字体并尝试使用 将其作为本地.ttf
文件应用时@font-face
,我无法获得相同的结果。
这是重要的文件,App.js
:
import React from "react";
import { Global, css } from "@emotion/core";
import styled from "@emotion/styled";
const GlobalStyles = css`
@import url("https://fonts.googleapis.com/css?family=Dancing+Script&display=swap");
@font-face {
font-family: "Local Font";
src: url("fonts/DancingScript-Regular.ttf");
}
* {
text-align: center;
}
`;
const FromGoogle = styled.h1`
font-family: "Dancing Script";
`;
const FromLocal = styled.h1`
font-family: "Local Font";
`;
function App() {
return (
<div className="App">
<Global styles={GlobalStyles} />
<FromGoogle>This text's font family is from Google.</FromGoogle>
<FromLocal>
This text's font family should be the same, except it comes from a local
font file, and it's not working.
</FromLocal>
</div>
);
}
export default App;
出于某种原因,文本中的文本FromGoogle
使用 Google 字体很好,而来自的文本FromLocal
则没有。我的第一个想法是这是路径的问题,但如果是,我无法判断。
这是GitHub 上的完整项目。我使用了 Create React App,并删除了所有不相关的样板。