Material-UI 中添加的自托管字体不起作用?

sai*_*oon 5 fonts font-face reactjs material-ui reaction-commerce

我正在开发一个基于 Material-UI 的项目,并尝试添加一些自托管字体,因为它们不能作为谷歌字体使用。我遵循了 Material-UI 和 JSS 文档,并提出了以下内容,但我找不到它仍然不起作用的原因。也没有任何错误可以让我知道出了什么问题。

import { createMuiTheme } from "@material-ui/core/styles";
import Windlass from "./fonts/Windlass.eot";
import Windlass2 from "./fonts/Windlass.woff";

const theme = createMuiTheme({
  ...
  typography: {
    useNextVariants: true,
    fontFamily: [
      'Windlass',
      'Helvetica Neue',
      'Helvetica'
    ].join(','),
    fontSize: 16,
    fontWeightLight: 400,
    fontWeightRegular: 400,
    fontWeightMedium: 600,
    fontWeightBold: 700},
  overrides: {
    MuiCssBaseline: {
      '@global': {
        '@font-face': {
          fontFamily: 'Windlass',
          src: `url(${Windlass})`,
          fallbacks: [
            {src: `url(${Windlass}?#iefix) format(embedded-opentype)`},
            {src: `url(${Windlass2}) format(woff)`}
          ]
          }
        }
      }
  }
});

export default theme;
Run Code Online (Sandbox Code Playgroud)

当我检查元素时,字体名称似乎确实显示在 css 中

font-family: Windlass,Helvetica Neue,Helvetica;
Run Code Online (Sandbox Code Playgroud)

但文本显示为 Helvetica Neue。

kun*_*hao 1

这是我的配置,它可以工作,您需要像这样在 index.css 中声明字体。

主题.js

import { createMuiTheme } from "@material-ui/core/styles";
// import gliberPath from "./Gilbert_Bold-Preview_1004.woff2"; // this is no need to add
let palette1 = {
  type: "light"
};

let palette2 = {
  type: "light"
};


// this is no need to add 
// const gliber = {
//   fontFamily: "Gliber",
//   fontStyle: "normal",
//   fontDisplay: "swap",
//   fontWeight: 400,
//   src: `
//       local('Gliber'),
//       url(${gliberPath}) format('woff2')
//     `,
// };

const typography = {
  fontFamily: ["Gliber","Roboto"].join(","), // there need add Gliber reference,your font family name should be place the first position
 // this is no need to add
  // overrides: {
  //   MuiCssBaseline: {
  //     "@global": {
  //       "@font-face": ['Gliber']
  //     }
  //   }
  // }
};

let theme1 = createMuiTheme({ palette: palette1, typography });

let theme2 = createMuiTheme({ palette: palette2, typography });

export { theme1, theme2 };

Run Code Online (Sandbox Code Playgroud)

索引.css

body {
    margin: 0;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen",
    "Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue",
    sans-serif;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

code {
    font-family: source-code-pro, Menlo, Monaco, Consolas, "Courier New",
    monospace;
}
/*add self host font */
@font-face {
    font-family: 'Gliber';
    font-style: normal;
    font-weight: 400;
    font-display: swap;
    src: url('./theme/Gilbert_Bold-Preview_1004.woff2') format('woff2');
  }
Run Code Online (Sandbox Code Playgroud)