【问题标题】:Self-host font added in Material-UI not working?Material-UI 中添加的自托管字体不起作用?
【发布时间】:2019-11-28 04:34:01
【问题描述】:

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

【问题讨论】:

    标签: reactjs fonts material-ui font-face reaction-commerce


    【解决方案1】:

    以防有人偶然发现这篇文章。 这是向 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],
          },
        },
      },
    });
    

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

    【讨论】:

    • 我不断收到 Module parse failed: Unexpected character '' (1:4) 您可能需要适当的加载程序来处理此文件类型,目前没有配置加载程序来处理此文件。见webpack.js.org/concepts#loaders
    • @insivika 我需要更多信息。如果您还没有找到解决方案,请提交问题。
    【解决方案2】:

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

    theme.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 };
    
    

    index.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');
      }
    

    【讨论】:

      猜你喜欢
      • 2021-11-20
      • 2019-10-10
      • 2020-01-28
      • 2021-04-29
      • 2019-09-05
      • 2014-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多