我们能否用更少的代码更改Material UI组件的字体族?我已经尝试了许多方法,但仍然无法做到。我必须单独更改字体族,这真的是很多工作量。有其他方法可以做到吗?
我们能否用更少的代码更改Material UI组件的字体族?我已经尝试了许多方法,但仍然无法做到。我必须单独更改字体族,这真的是很多工作量。有其他方法可以做到吗?
<App />
如下所示定义:// Material UI
import { MuiThemeProvider, createMuiTheme } from 'material-ui/styles';
const App = () => (
<MuiThemeProvider theme={THEME}>
<Provider store={store}>
<Router history={appHistory} routes={Routes} />
</Provider>
</MuiThemeProvider>
);
ReactDOM.render(<App />, document.getElementById('app'));
在 MuiThemeProvider
的 theme
属性中,您提供了以下内容。const THEME = createMuiTheme({
typography: {
"fontFamily": `"Roboto", "Helvetica", "Arial", sans-serif`,
"fontSize": 14,
"fontWeightLight": 300,
"fontWeightRegular": 400,
"fontWeightMedium": 500
}
});
然后在你的css
或主要的index.html
文件中包含这个:@import url('https://fonts.googleapis.com/css?family=Roboto:300,400,500,700');
关于你可以为createMuiTheme
提供的所有参数列表,请参阅默认主题参数。有关更改 MuiTheme 的文档本身,它们如下所示。Material UI Next 主题
关于<Reboot />
部分,你可以在这里查看文档 Material UI Reboot 详情
div
标签使用自定义字体,则需要单独指定它。 - Adeel Imrantypography
对象中添加以下内容。typography: { useNextVariants: true }
- Adeel Imran**** 更新 *****
在Adeel的回答基础上做出补充。
对于最新的Material-UI(v4+)组件,导入语句和MuiThemeProvider
已经发生了变化。
现在,在你的App.js
文件中,按照以下方式操作:
import { createMuiTheme } from '@material-ui/core/styles';
import { ThemeProvider } from '@material-ui/styles';
import './Styles/App.css';
const theme = createMuiTheme({
typography: {
fontFamily: [
'Nunito',
'Roboto',
'"Helvetica Neue"',
'Arial',
'sans-serif'
].join(','),
}
});
class App extends Component {
render() {
return (
<ThemeProvider theme={theme}>
<div className="App">
<MainApp />
</div>
</ThemeProvider>
);
}
}
export default App;
比如说,现在我添加了Nunito
字体。因此,我需要在App.css
中以以下方式添加相同的字体(该文件被导入到App.js
中):
@font-face {
font-family: 'Nunito';
font-style: normal;
font-weight: 400;
font-display: swap;
src: local('Nunito Regular'), local('Nunito-Regular'),
url(https://fonts.gstatic.com/s/nunito/v11/XRXV3I6Li01BKofINeaBTMnFcQ.woff2)
format('woff2');
unicode-range: 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, U+FFFD;
}
在 MUI v5 中,请确保从 @mui/material/styles
导入 ThemeProvider 和 createTheme,而不是从 @mui/styles
。我曾经因为这个问题卡了几个小时。
import { ThemeProvider, createTheme } from '@mui/styles'; ❌
import { ThemeProvider, createTheme } from '@mui/material/styles'; ✅
const theme = createTheme({
typography: {
allVariants: {
fontFamily: 'serif',
textTransform: 'none',
fontSize: 16,
},
},
});
function App() {
return (
<ThemeProvider theme={theme}>
...
</ThemeProvider>
);
}
Typography
变体的 fontFamily
或任何其他 CSS 属性:const theme = createTheme({
typography: {
allVariants: {
fontFamily: 'serif',
},
},
})
fontFamily
,可以使用useMemo
根据最新的fontFamily
值创建一个新的theme
对象:const defaultFontFamily = 'serif';
const [fontFamily, setFontFamily] = React.useState(defaultFontFamily);
const theme = React.useMemo(
() =>
createTheme({
typography: {
allVariants: { fontFamily },
},
}),
[fontFamily],
);
return (
<div>
<Select
defaultValue={defaultFontFamily}
onChange={(e) => setFontFamily(e.target.value)}
>
<MenuItem value="serif">serif</MenuItem>
<MenuItem value="sans-serif">sans-serif</MenuItem>
</Select>
<ThemeProvider theme={theme}>
<Content />
</ThemeProvider>
</div>
);
createMuiTheme
中声明样式时,逗号和括号需要非常小心。很容易出错。例如,调色板是一个大对象……排版也是,确保所有内容都放在正确的位置。我曾因此犯过错误而遇到了随机问题。 palette: {
primary: {
light: '#ff8e8c',
main: '#ff5a5f',
dark: '#c62035',
contrastText: '#fff',
},
secondary: {
light: '#4da9b7',
main: '#017a87',
dark: '#004e5a',
contrastText: '#000',
},
},
typography: {
allVariants: {
fontFamily: "'Montserrat', sans-serif",
textTransform: "none",
}
button: {
textTransform: "none",
}
},
const theme = createMuiTheme({
typography: {
fontFamily: [
'-apple-system',
'BlinkMacSystemFont',
'"Segoe UI"',
'Roboto',
'"Helvetica Neue"',
'Arial',
'sans-serif',
'"Apple Color Emoji"',
'"Segoe UI Emoji"',
'"Segoe UI Symbol"',
].join(','),
},
});
import { createTheme, responsiveFontSizes } from '@mui/material/styles';
export default function GlobalStyleOverrides() {
const theme = createTheme({
typography: {
fontFamily: [
'"Bebas Neue"',
'Roboto',
'"Helvetica Neue"',
'Arial',
'sans-serif',
].join(','),
body1: {
fontFamily: "'Poppins', Arial, sans-serif",
},
},
components: {
MuiButton: {
variants: [
{
props: { variant: 'contained' },
style: {
textTransform: 'none',
border: `10px dashed red`,
},
},
],
},
},
});
return responsiveFontSizes(theme);
}
App.js
import GlobalStyleOverrides from 'components/_base/global-style-overrides/GlobalStyleOverrides';
...
function App() {
return (
<ThemeProvider theme={GlobalStyleOverrides()}>
<Router>
...
</Router>
</ThemeProvider>
);
}
当使用中间主题时,将字体放在那里非常重要,例如:
import { createTheme } from "@mui/material/styles";
import { Inter } from "next/font/google";
export const inter = Inter({
subsets: ["latin"],
});
let theme = createTheme({
typography: {
allVariants: {
fontFamily: inter.style.fontFamily,
},
},
breakpoints: {
values: {
xs: 0,
sm: 656,
md: 1023,
lg: 1440,
xl: 1920,
},
},
});
const Mytheme = createTheme(theme, {
typography: {
h1: {
fontWeight: 700,
[theme.breakpoints.between("xs", "md")]: {
fontSize: "40px",
lineHeight: "48px",
},
[theme.breakpoints.between("md", "xl")]: {
fontSize: "56px",
lineHeight: "64px",
},
},
h2: {
fontWeight: 700,
fontSize: "40px",
lineHeight: "48px",
},
h3: {
fontWeight: 700,
fontSize: "32px",
lineHeight: "40px",
},
},
});
export default Mytheme;
在MUI 5之后,方法有些许改变。
对于您的用例,良好的做法是创建一个全局主题对象并根据您的需求进行调整。请注意,您也可以微调不同的排版变体!
import {createTheme} from "@mui/material/styles";
const theme = createTheme(
{
typography: {
fontFamily: ['Merriweather', 'serif'].join(','),
fontSize: 16,
h2: {
fontSize: "60px",
fontWeight: "bold",
}
},
}
)
import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';
import theme from "./theme";
import {ThemeProvider} from "@mui/material/styles";
const root = ReactDOM.createRoot(
document.getElementById('root') as HTMLElement
);
root.render(
<React.StrictMode>
<ThemeProvider theme={theme}>
<App/>
</ThemeProvider>
</React.StrictMode>
);
重要!
在使用外部字体时,请确保也导入它。 例如:在 "index.html" 中添加以下代码 -
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Merriweather:wght@300;700&display=swap" rel="stylesheet">