Material-UI 中添加的自托管字体不起作用?
Self-host font added in Material-UI not working?
我正在开发一个基于 Material-UI 的项目并尝试添加一些自托管字体,因为它们不能用作 google 字体。我已经关注了 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。
这是我的配置并且可以正常工作,您需要像这样在 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');
}
以防有人无意中发现这个post。
这是向 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],
},
},
},
});
设置完成后。在浏览器上打开您的开发人员工具并验证它们确实正在被下载。这对我有用。希望对你有用。
我正在开发一个基于 Material-UI 的项目并尝试添加一些自托管字体,因为它们不能用作 google 字体。我已经关注了 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。
这是我的配置并且可以正常工作,您需要像这样在 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');
}
以防有人无意中发现这个post。 这是向 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],
},
},
},
});
设置完成后。在浏览器上打开您的开发人员工具并验证它们确实正在被下载。这对我有用。希望对你有用。