React 18 -MUI-更改字体家庭

发布于 2025-02-12 05:41:04 字数 673 浏览 0 评论 0原文

我创建了一个主题来更改MUI的默认字体家族 但这无效。

这是我的主题组件:

import { createTheme } from "@mui/material/styles";

const theme = createTheme({
  typography: {
    fontFamily: ["samim", "vazir"].join(","),
  },
});

export default theme;

我在我的主app.tsx中使用主题组件来更改项目所有部分的字体

import React from "react";
import { ThemeProvider } from "@material-ui/styles";
import theme from "./theme/theme";
import Routes from "./routes/Routs";

const App = () => {
  return (
    <ThemeProvider theme={theme}>
      <Routes />;
    </ThemeProvider>
  );
};

export default App;

,任何人可以帮助我吗?

I create a theme to change the default font family of MUI
But it didn't work.

this is my theme component:

import { createTheme } from "@mui/material/styles";

const theme = createTheme({
  typography: {
    fontFamily: ["samim", "vazir"].join(","),
  },
});

export default theme;

I use the theme component in my main app.tsx to change the font in all parts of my project

import React from "react";
import { ThemeProvider } from "@material-ui/styles";
import theme from "./theme/theme";
import Routes from "./routes/Routs";

const App = () => {
  return (
    <ThemeProvider theme={theme}>
      <Routes />;
    </ThemeProvider>
  );
};

export default App;

can anyone help me, please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

大海や 2025-02-19 05:41:04

更改整个项目字体家族的最佳方法是使用MUI的 csssbaseline

theme.ts:

import { createTheme } from '@mui/material/styles';
import samim from "./assets/fonts/samim.ttf";

const theme = createTheme({
   typography: {
    "fontFamily": `"samim", "vazir"`,   
   }, 
   components: {
    MuiCssBaseline: {
      styleOverrides: `
        @font-face {
          font-family: 'samim';
          font-style: normal;
          font-display: swap;
          font-weight: 400;
          src: local('samim'), local('samim'), url(${samim}) format('truetype');
          unicodeRange: 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;
        }
      `,
    },
});

app.tsx:

import React from "react";
import { ThemeProvider } from "@material-ui/styles";
import theme from "./theme/theme";
import Routes from "./routes/Routs";

const App = () => {
  return (
    <ThemeProvider theme={theme}>
      <CssBaseline />
      <Routes />;
    </ThemeProvider>
  );
};

export default App;

The best way to change the font family of entire project is using MUI's CSSBaseline.

theme.ts:

import { createTheme } from '@mui/material/styles';
import samim from "./assets/fonts/samim.ttf";

const theme = createTheme({
   typography: {
    "fontFamily": `"samim", "vazir"`,   
   }, 
   components: {
    MuiCssBaseline: {
      styleOverrides: `
        @font-face {
          font-family: 'samim';
          font-style: normal;
          font-display: swap;
          font-weight: 400;
          src: local('samim'), local('samim'), url(${samim}) format('truetype');
          unicodeRange: 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;
        }
      `,
    },
});

app.tsx:

import React from "react";
import { ThemeProvider } from "@material-ui/styles";
import theme from "./theme/theme";
import Routes from "./routes/Routs";

const App = () => {
  return (
    <ThemeProvider theme={theme}>
      <CssBaseline />
      <Routes />;
    </ThemeProvider>
  );
};

export default App;
残花月 2025-02-19 05:41:04

您可以尝试一下。

准备如下的主题组件。

import { createTheme } from '@mui/material/styles';
const theme = createTheme({
   typography: {
    "fontFamily": `"samim", "vazir"`,   
   }
});

然后,您可以在以下内容中应用于app.js。

    import theme from "./theme/theme";
    import Routes from "./routes/Routes";
    import { ThemeProvider } from '@mui/material/styles';

    const App = () => (
      <ThemeProvider theme={theme}>
         <Routes />;
      </ThemeProvider>
    );

ReactDOM.render(<App />, document.getElementById('app'));
``

I guess that issue was belong to ThemeProvider...

You can try this.

Prepare theme component like below.

import { createTheme } from '@mui/material/styles';
const theme = createTheme({
   typography: {
    "fontFamily": `"samim", "vazir"`,   
   }
});

Then you can apply this in app.js like below.

    import theme from "./theme/theme";
    import Routes from "./routes/Routes";
    import { ThemeProvider } from '@mui/material/styles';

    const App = () => (
      <ThemeProvider theme={theme}>
         <Routes />;
      </ThemeProvider>
    );

ReactDOM.render(<App />, document.getElementById('app'));
``

I guess that issue was belong to ThemeProvider...
萌辣 2025-02-19 05:41:04

创建的代码沙盒演示用于自定义材料UI主题,
demo link

您在哪里使用文本进行主题来进行

官方文档的链接
https://mui.com/material-ui/customization y/customization/typography/

也,您需要从 @mui/材料/样式导入theprovider

Created Code sandbox Demo for customizing Material UI Theme,
Demo Link

You would need to use Typography component where you are using text for theme part to take effect

Link from official Docs
https://mui.com/material-ui/customization/typography/

Also, you would need to import ThemeProvider from @mui/material/styles

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文