安装页面只是引用Google字体页面, 但这显然会强制从Google服务器下载字体。
关于Roboto字体存在一个类似的Material UI问题, 但仍然依赖于Google提供字体文件。
我发现了一个NPM包提供Roboto字体文件, 但我不确定如何将这些文件包含进来,因为提供了许多样式和字体格式,我不知道Material UI真正需要哪些样式。而且,仅通过@import导入这些字体系列似乎会有性能问题。
那么,有什么好的简单解决方案可以将适当的Roboto文件与我的应用程序捆绑在一起呢?
安装页面只是引用Google字体页面, 但这显然会强制从Google服务器下载字体。
关于Roboto字体存在一个类似的Material UI问题, 但仍然依赖于Google提供字体文件。
我发现了一个NPM包提供Roboto字体文件, 但我不确定如何将这些文件包含进来,因为提供了许多样式和字体格式,我不知道Material UI真正需要哪些样式。而且,仅通过@import导入这些字体系列似乎会有性能问题。
那么,有什么好的简单解决方案可以将适当的Roboto文件与我的应用程序捆绑在一起呢?
以下是我的团队在 Webpack 项目中添加 Roboto 字体的具体步骤:
/fonts
)。/fonts
文件夹中。/fonts/index.css
),我们从这篇文章获取了该文件的内容index.css:
* {
font-family: Roboto, sans-serif;
}
@font-face {
font-family: 'Roboto';
src: url('Roboto-Regular-webfont.eot');
src: url('Roboto-Regular-webfont.eot?#iefix') format('embedded-opentype'),
url('Roboto-Regular-webfont.woff') format('woff'),
url('Roboto-Regular-webfont.ttf') format('truetype'),
url('Roboto-Regular-webfont.svg#RobotoRegular') format('svg');
font-weight: normal;
font-style: normal;
}
@font-face {
font-family: 'Roboto';
src: url('Roboto-Italic-webfont.eot');
src: url('Roboto-Italic-webfont.eot?#iefix') format('embedded-opentype'),
url('Roboto-Italic-webfont.woff') format('woff'),
url('Roboto-Italic-webfont.ttf') format('truetype'),
url('Roboto-Italic-webfont.svg#RobotoItalic') format('svg');
font-weight: normal;
font-style: italic;
}
@font-face {
font-family: 'Roboto';
src: url('Roboto-Bold-webfont.eot');
src: url('Roboto-Bold-webfont.eot?#iefix') format('embedded-opentype'),
url('Roboto-Bold-webfont.woff') format('woff'),
url('Roboto-Bold-webfont.ttf') format('truetype'),
url('Roboto-Bold-webfont.svg#RobotoBold') format('svg');
font-weight: bold;
font-style: normal;
}
@font-face {
font-family: 'Roboto';
src: url('Roboto-BoldItalic-webfont.eot');
src: url('Roboto-BoldItalic-webfont.eot?#iefix') format('embedded-opentype'),
url('Roboto-BoldItalic-webfont.woff') format('woff'),
url('Roboto-BoldItalic-webfont.ttf') format('truetype'),
url('Roboto-BoldItalic-webfont.svg#RobotoBoldItalic') format('svg');
font-weight: bold;
font-style: italic;
}
@font-face {
font-family: 'Roboto';
src: url('Roboto-Thin-webfont.eot');
src: url('Roboto-Thin-webfont.eot?#iefix') format('embedded-opentype'),
url('Roboto-Thin-webfont.woff') format('woff'),
url('Roboto-Thin-webfont.ttf') format('truetype'),
url('Roboto-Thin-webfont.svg#RobotoThin') format('svg');
font-weight: 200;
font-style: normal;
}
@font-face {
font-family: 'Roboto';
src: url('Roboto-ThinItalic-webfont.eot');
src: url('Roboto-ThinItalic-webfont.eot?#iefix') format('embedded-opentype'),
url('Roboto-ThinItalic-webfont.woff') format('woff'),
url('Roboto-ThinItalic-webfont.ttf') format('truetype'),
url('Roboto-ThinItalic-webfont.svg#RobotoThinItalic') format('svg'); (under the Apache Software License).
font-weight: 200;
font-style: italic;
}
@font-face {
font-family: 'Roboto';
src: url('Roboto-Light-webfont.eot');
src: url('Roboto-Light-webfont.eot?#iefix') format('embedded-opentype'),
url('Roboto-Light-webfont.woff') format('woff'),
url('Roboto-Light-webfont.ttf') format('truetype'),
url('Roboto-Light-webfont.svg#RobotoLight') format('svg');
font-weight: 100;
font-style: normal;
}
@font-face {
font-family: 'Roboto';
src: url('Roboto-LightItalic-webfont.eot');
src: url('Roboto-LightItalic-webfont.eot?#iefix') format('embedded-opentype'),
url('Roboto-LightItalic-webfont.woff') format('woff'),
url('Roboto-LightItalic-webfont.ttf') format('truetype'),
url('Roboto-LightItalic-webfont.svg#RobotoLightItalic') format('svg');
font-weight: 100;
font-style: italic;
}
@font-face {
font-family: 'Roboto';
src: url('Roboto-Medium-webfont.eot');
src: url('Roboto-Medium-webfont.eot?#iefix') format('embedded-opentype'),
url('Roboto-Medium-webfont.woff') format('woff'),
url('Roboto-Medium-webfont.ttf') format('truetype'),
url('Roboto-Medium-webfont.svg#RobotoMedium') format('svg');
font-weight: 300;
font-style: normal;
}
@font-face {
font-family: 'Roboto';
src: url('Roboto-MediumItalic-webfont.eot');
src: url('Roboto-MediumItalic-webfont.eot?#iefix') format('embedded-opentype'),
url('Roboto-MediumItalic-webfont.woff') format('woff'),
url('Roboto-MediumItalic-webfont.ttf') format('truetype'),
url('Roboto-MediumItalic-webfont.svg#RobotoMediumItalic') format('svg');
font-weight: 300;
font-style: italic;
}
npm install --save file-loader
(https://www.npmjs.com/package/file-loader)webpack.conf.js:
loaders: [
..., {
test: /\.(woff|woff2|eot|ttf|svg)$/,
loader: 'file-loader',
options: { name: '[name].[ext]', outputPath: 'fonts/', }
},
...
]
App.js:
import './fonts/index.css';
就这样了。现在你的应用程序默认字体应该是Roboto了。
这个问题的一部分是确定在项目中包含哪些正确的 Roboto 字体,因为整个 Roboto 字体库几乎有 5MB 大小。
在README中,包含 Roboto 的说明指向:fonts.google.com/?selection.family=Roboto:300,400,500。这里,300 = Roboto-Light,400 = Roboto-Regular,500 = Roboto-Medium。这些对应于在 typography.js 文件 中定义的字重。虽然这三种字重几乎涵盖了整个库中的使用,但在DateDisplay.js 中有一个与 Regular-Bold 相关的引用。如果您不使用 DatePicker,则可以安全地忽略它。斜体字样式除了 GitHub markdown 样式之外,在项目中没有任何使用。
此信息在撰写本文时是准确的,但可能在将来发生变化。
{
test: /\.(woff|woff2|eot|ttf|svg)$/,
loader: 'file-loader',
options: {
name: '[name].[ext]',
outputPath: 'fonts/',
},
},
否则可能会出现问题。 - Ser您也可以按照此问题的文档进行操作: https://github.com/callemall/material-ui/issues/6256
npm install typeface-roboto --save
然后,在你的index.js文件中:
import 'typeface-roboto'
适用于webpack/create-react-app。
import typeface-roboto
?例如,如果我只想要300和400字体。 - vogdb模块解析失败:意外字符'@'。您可能需要一个适当的加载器来处理此文件类型,目前没有配置任何加载器
。 - 425nesp这是根据MUI文档所述的;我这样做了,它完美地运行了。
使用npm进行安装:
npm install @fontsource/roboto
... 或使用 yarn:
yarn add @fontsource/roboto
然后,在你的应用程序入口点中包含这些导入项,例如 index.tsx
:
import '@fontsource/roboto/300.css';
import '@fontsource/roboto/400.css';
import '@fontsource/roboto/500.css';
import '@fontsource/roboto/700.css';
获取更多信息,请阅读文档
。我尝试使用npm安装typeface-roboto,但它没有起作用。同时,使用material ui的CDN也无法正常工作。但是,使用npm安装webfontloader可以解决问题。以下是解决方案:
首先,
npm install webfontloader --save
然后,在您的entry.js文件(例如App.js或index.js)中从webfontloader中导入WebFont。
import WebFont from "webfontloader";
WebFont.load({google: {families: ["Roboto:300,400,500"]}});
import 'typeface-roboto'
不是最佳选择,也不太容易。你可以按照以下建议进行操作。npm install typeface-roboto --save
angular.json
文件中:"styles": [
"node_modules/typeface-roboto/index.css",
[...],
"src/styles.css"
],
如果应用程序是使用create-react-app启动的,则没有[可见的]webpack配置文件。在这种情况下,您可以执行以下操作:
创建/public/fonts/fonts.css,定义@font-faces
@font-face {
font-family: 'inglobal';
font-weight: normal;
font-style: normal;
src: url('./OperatorMono.ttf');
}
复制字体文件
将<link rel="stylesheet" href="%PUBLIC_URL%/fonts/fonts.css">
添加到/public/index.html的
太棒了!
5/b. 如果由于任何原因仍然无法正常工作,请将字体扩展名更改为.css(也要在src: url('./OperatorMono.css')中)
对于一个简单的 Material-UI / Create React App PWA,不需要所有的变体 - 你只需要:
yarn add @fontsource/roboto
index.js
import "@fontsource/roboto/latin-400.css";
import "@fontsource/roboto/latin-500.css";
theme.js(可选,但使其看起来更清晰、整洁)
overrides: {
MuiCssBaseline: {
html: {
"-webkit-font-smoothing": "antialiased",
"-moz-osx-font-smoothing": "grayscale",
height: "100%",
width: "100%"
}
}
}
您的字体将被捆绑并立即在线/离线提供
虽然我目前在我的Vite/React/MUI项目中没有使用Webpack,但是这里是我如何以简单的方式集成Roboto字体的方法:
我发现你可以在这里下载字体的可变版本: https://fonts.google.com/specimen/Roboto+Flex
然后我只需像这样将它添加到我的主题中:
import React from 'react'
import ReactDOM from 'react-dom/client'
import App from './App.jsx'
import './index.css'
import { ThemeProvider } from '@emotion/react'
import { CssBaseline, createTheme } from '@mui/material'
import RobotoFlex from './fonts/Roboto_Flex/RobotoFlex-VariableFont_GRAD,XTRA,YOPQ,YTAS,YTDE,YTFI,YTLC,YTUC,opsz,slnt,wdth,wght.ttf'
const theme = createTheme({
typography: {
fontFamily: 'Roboto',
},
components: {
MuiCssBaseline: {
styleOverrides: `
@font-face {
font-family: 'Roboto';
font-style: normal;
src: local('Roboto'), local('Roboto-Regular'), url(${RobotoFlex}) format('woff2');
unicodeRange: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF;
}
`,
},
},
});
ReactDOM.createRoot(document.getElementById('root')).render(
<React.StrictMode>
<ThemeProvider theme={theme}>
<CssBaseline />
<App />
</ThemeProvider>
</React.StrictMode>,
)