我正在使用创建-反应-应用程序,不喜欢弹出。
不清楚通过@font-face导入并在本地加载的字体应该放在哪里。
也就是说,我在装载
@font-face {
font-family: 'Myriad Pro Regular';
font-style: normal;
font-weight: normal;
src: local('Myriad Pro Regular'), url('MYRIADPRO-REGULAR.woff') format('woff');
}
有什么建议吗?
——编辑
包括丹在回答中提到的要点
➜ Client git:(feature/trivia-game-ui-2) ✗ ls -l public/static/fonts
total 1168
-rwxr-xr-x@ 1 maximveksler staff 62676 Mar 17 2014 MYRIADPRO-BOLD.woff
-rwxr-xr-x@ 1 maximveksler staff 61500 Mar 17 2014 MYRIADPRO-BOLDCOND.woff
-rwxr-xr-x@ 1 maximveksler staff 66024 Mar 17 2014 MYRIADPRO-BOLDCONDIT.woff
-rwxr-xr-x@ 1 maximveksler staff 66108 Mar 17 2014 MYRIADPRO-BOLDIT.woff
-rwxr-xr-x@ 1 maximveksler staff 60044 Mar 17 2014 MYRIADPRO-COND.woff
-rwxr-xr-x@ 1 maximveksler staff 64656 Mar 17 2014 MYRIADPRO-CONDIT.woff
-rwxr-xr-x@ 1 maximveksler staff 61848 Mar 17 2014 MYRIADPRO-REGULAR.woff
-rwxr-xr-x@ 1 maximveksler staff 62448 Mar 17 2014 MYRIADPRO-SEMIBOLD.woff
-rwxr-xr-x@ 1 maximveksler staff 66232 Mar 17 2014 MYRIADPRO-SEMIBOLDIT.woff
➜ Client git:(feature/trivia-game-ui-2) ✗ cat src/containers/GameModule.css
.GameModule {
padding: 15px;
}
@font-face {
font-family: 'Myriad Pro Regular';
font-style: normal;
font-weight: normal;
src: local('Myriad Pro Regular'), url('%PUBLIC_URL%/static/fonts/MYRIADPRO-REGULAR.woff') format('woff');
}
@font-face {
font-family: 'Myriad Pro Condensed';
font-style: normal;
font-weight: normal;
src: local('Myriad Pro Condensed'), url('%PUBLIC_URL%/static/fonts/MYRIADPRO-COND.woff') format('woff');
}
@font-face {
font-family: 'Myriad Pro Semibold Italic';
font-style: normal;
font-weight: normal;
src: local('Myriad Pro Semibold Italic'), url('%PUBLIC_URL%/static/fonts/MYRIADPRO-SEMIBOLDIT.woff') format('woff');
}
@font-face {
font-family: 'Myriad Pro Semibold';
font-style: normal;
font-weight: normal;
src: local('Myriad Pro Semibold'), url('%PUBLIC_URL%/static/fonts/MYRIADPRO-SEMIBOLD.woff') format('woff');
}
@font-face {
font-family: 'Myriad Pro Condensed Italic';
font-style: normal;
font-weight: normal;
src: local('Myriad Pro Condensed Italic'), url('%PUBLIC_URL%/static/fonts/MYRIADPRO-CONDIT.woff') format('woff');
}
@font-face {
font-family: 'Myriad Pro Bold Italic';
font-style: normal;
font-weight: normal;
src: local('Myriad Pro Bold Italic'), url('%PUBLIC_URL%/static/fonts/MYRIADPRO-BOLDIT.woff') format('woff');
}
@font-face {
font-family: 'Myriad Pro Bold Condensed Italic';
font-style: normal;
font-weight: normal;
src: local('Myriad Pro Bold Condensed Italic'), url('%PUBLIC_URL%/static/fonts/MYRIADPRO-BOLDCONDIT.woff') format('woff');
}
@font-face {
font-family: 'Myriad Pro Bold Condensed';
font-style: normal;
font-weight: normal;
src: local('Myriad Pro Bold Condensed'), url('%PUBLIC_URL%/static/fonts/MYRIADPRO-BOLDCOND.woff') format('woff');
}
@font-face {
font-family: 'Myriad Pro Bold';
font-style: normal;
font-weight: normal;
src: local('Myriad Pro Bold'), url('%PUBLIC_URL%/static/fonts/MYRIADPRO-BOLD.woff') format('woff');
}
当使用不同的字体文件为正常/斜体字体风格时,您指定@font-face的方式可能需要根据入口点而不同。请看我的回答:
如果你选择将css文件直接链接到public/index.html,那么你通常可以使用一个字体家族名称和不同的字体风格的font-face:
@font-face {
font-family: "FontName"; <---
font-style: normal; <---
font-weight: normal;
src: url("path-to-assets/fonts/FontName.ttf") format("truetype");
}
@font-face {
font-family: "FontName"; <---
font-style: italic; <---
font-weight: normal;
src: url("path-to-assets/fonts/FontName-Italic.ttf") format("truetype");
}
/* Usage */
.text {
font-family: FontName;
font-style: normal;
}
.text-italic {
font-family: FontName;
font-style: italic;
}
如果你选择通过Js链接css文件进行捆绑,那么你需要为所有斜体字体设置一个不同的字体族名称,并使用正常的字体样式。
@font-face {
font-family: "FontName"; <---
font-style: normal; <---
font-weight: normal; /* normal | 300 | 400 | 600 | bold | etc */
src: url("path-to-assets/fonts/FontName.ttf") format("truetype");
}
@font-face {
font-family: "FontNameItalic";
font-style: normal; <----
font-weight: normal; /* normal | 300 | 400 | 600 | bold | etc */
src: url("path-to-assets/fonts/FontName-Italic.ttf") format("truetype");
}
/* Usage */
.text {
font-family: FontName;
}
.text-italic {
font-family: FontNameItalic;
}
我犯了这样的错误。
@import "https://fonts.googleapis.com/css?family=Open+Sans:300,300i,400,400i,600,600i,700,700i&subset=cyrillic,cyrillic-ext,latin-ext";
@import "https://use.fontawesome.com/releases/v5.3.1/css/all.css";
它以这种方式正常工作
@import url(https://fonts.googleapis.com/css?family=Open+Sans:300,300i,400,400i,600,600i,700,700i&subset=cyrillic,cyrillic-ext,latin-ext);
@import url(https://use.fontawesome.com/releases/v5.3.1/css/all.css);
我的感觉是,如果你在你的网站上有本地的字体,你应该能够使用CSS字体加载API在运行时为你做字体加载,而不使用CSS文件。这是我写的一个React组件。它是基于上面user-rebo的答案,但尝试在使用后卸载字体。它似乎可以在火狐和Chrome浏览器中运行。代码是:
import React, { useEffect, PropsWithChildren, useRef } from "react";
type FontFaceFormat =
| "woff"
| "woff2"
| "truetype"
| "opentype"
| "embedded-opentype"
| "svg";
type FontWrapperProps = {
fontName: string;
fontURL: string;
fontFormat?: FontFaceFormat;
};
/*
* The FontWrapper class. Takes a fontName (like "Andika"), a fontURL (which
* should go to the website's public folder), and an optional font type. Usage
* should be as simple as:
*
* <FontWrapper fontName="Andika" fontURL="/fonts/Andika-Regular.ttf.woff2">
* <p>This is some text</p>
* </FontWrapper>
*
* Note: document.fonts is of type FontFaceSet, which should include the add() and
* delete() methods. However, the TypeScript libraries for Visual Studio Code
* miss these. That's why I have two instances of "document.fonts as any" -
* to make those red lines go away.
*/
export default function FontWrapper({
fontName,
fontURL,
fontFormat = "woff2",
children,
}: PropsWithChildren<FontWrapperProps>) {
const fontRef = useRef(null);
useEffect(() => {
const loadFont = async () => {
const font = new FontFace(
fontName,
`url(${fontURL}) format("${fontFormat}")`,
{}
);
const fontResult = await font.load();
fontRef.current = fontResult;
await (document.fonts as any).add(font);
};
const unloadFont = async () => {
await (document.fonts as any).delete(fontRef.current);
};
loadFont();
return () => {
unloadFont();
};
}, []);
return <div style={{ fontFamily: fontName }}>{children}</div>;
}
用法是:
<FontWrapper fontName="Andika" fontURL="/fonts/Andika-Regular.ttf.woff2">
<p>This is some text</p>
</FontWrapper>
去谷歌字体https://fonts.google.com/
选择如下图所示的字体:
复制并粘贴该url在新选项卡,你会得到css代码添加该字体。在这种情况下,如果你去
https://fonts.googleapis.com/css?family=Spicy+Rice
它会像这样打开:
4、复制并粘贴到你的style.css中,然后像这样简单地开始使用那种字体:
<Typography
variant="h1"
gutterBottom
style={{ fontFamily: "Spicy Rice", color: "pink" }}
>
React Rock
</Typography>
结果: