4

我正在开发一个基于 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;

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

font-family: Windlass,Helvetica Neue,Helvetica;

但文本显示为 Helvetica Neue。

4

2 回答 2

2

以防有人偶然发现这篇文章。这是向 Material UI 主题添加字体的另一种方法。我按照 MUI 网站上的步骤进行操作。

https://material-ui.com/customization/typography/

///myAwesomeFont.ts
import myAwesomeFontEot from '../assets/fonts/myAwesomeFont.eot';
import myAwesomeFontWoff from '../assets/fonts/myAwesomeFont.woff';
import myAwesomeFontWoff2 from '../assets/fonts/myAwesomeFont.woff2';


import myAwesomeFontBoldEot from '../assets/fonts/myAwesomeFont-Bold.eot';
import myAwesomeFontBoldWoff from '../assets/fonts/myAwesomeFont-Bold.woff';
import myAwesomeFontBoldWoff2 from '../assets/fonts/myAwesomeFont-Bold.woff2';


const myAwesomeFontRegular = {
  fontFamily: 'myAwesomeFont',
  fontStyle: 'normal',
  fontWeight: 400,
  src: `local('myAwesomeFont-Regular'), 
  url(${myAwesomeFontEot}),
  url(${myAwesomeFontWoff}) format('woff'),
  url(${myAwesomeFontWoff2}) format('woff2')`,
};

const myAwesomeFontBold = {
  fontFamily: 'myAwesomeFont',
  fontStyle: 'normal',
  fontWeight: 700,
  src: `local('myAwesomeFont-Bold'), 
  url(${myAwesomeFontEot}),
  url(${myAwesomeFontWoff}) format('woff'),
  url(${myAwesomeFontWoff2}) format('woff2')`,
};

export default [myAwesomeFontRegular, myAwesomeFontBold]

在您的主题提供程序文件中。

//default.theme.ts
import { createMuiTheme, Theme } from '@material-ui/core/styles';
import myAwesomeFontArray from './myAwesomeFont';


const theme: Theme = createMuiTheme({
typography: {
    fontFamily: "myAwesomeFont, sans-serif",
    fontSize: 16
},
  overrides: {
    MuiCssBaseline: {
      '@global': {
        '@font-face': [...myAwesomeFontArray],
      },
    },
  },
});

设置好之后。在浏览器上打开您的开发人员工具并验证它们是否确实被下载。这对我有用。我希望这个对你有用。

于 2020-05-13T21:42:51.243 回答
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 };

索引.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');
  }
于 2019-08-11T14:08:00.160 回答