最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to add custom font weights to Material UI? - Stack Overflow

programmeradmin0浏览0评论

Trying to configure Material theme to use my own font and custom font weights/sizes for the Typography components. For the fontWeight section, I want to just be able to input 100/200/300/400/500/600/700 as options for each specific material typography variant, however, it specifically takes in a "string" and it apparently can only be normal/bold/bolder/lighter

And to make it worse normal == 400 while bold == 700 skipping over 500 and 600 which I need

typography: {
    fontFamily: "MyCustomFont",
    fontWeightLight: 200,
    fontWeightRegular: 400,
    fontWeightMedium: 500,
    useNextVariants: true,
    h1: {
      fontSize: "1.25rem",
      fontWeight: "bold",
      lineHeight: "1.2em"
    },
    h2: {
      fontSize: "1.75rem",
      fontWeight: "normal",
      lineHeight: "1.2em"
    },
}

Trying to configure Material theme to use my own font and custom font weights/sizes for the Typography components. For the fontWeight section, I want to just be able to input 100/200/300/400/500/600/700 as options for each specific material typography variant, however, it specifically takes in a "string" and it apparently can only be normal/bold/bolder/lighter

And to make it worse normal == 400 while bold == 700 skipping over 500 and 600 which I need

typography: {
    fontFamily: "MyCustomFont",
    fontWeightLight: 200,
    fontWeightRegular: 400,
    fontWeightMedium: 500,
    useNextVariants: true,
    h1: {
      fontSize: "1.25rem",
      fontWeight: "bold",
      lineHeight: "1.2em"
    },
    h2: {
      fontSize: "1.75rem",
      fontWeight: "normal",
      lineHeight: "1.2em"
    },
}
Share Improve this question asked Apr 15, 2019 at 15:54 user10741122user10741122 8911 gold badge16 silver badges29 bronze badges 1
  • Are you trying to style a Typography element like this: <Typography variant="h6" color="inherit"> or are you trying to style a material-ui element that contain text? – Pedro Vieira Commented Apr 15, 2019 at 16:48
Add a comment  | 

2 Answers 2

Reset to default 16

I am using the same behavior, with numbers, tested with all the 100/200/300/400/500/600/700 and it worked as well:

import { MuiThemeProvider, createMuiTheme } from '@material-ui/core/styles';
import { Typography } from '@material-ui/core/';

const THEME = createMuiTheme({
  typography: {
    "fontFamily": "\"MyCustomFont\"",
    "fontSize": 20,
    "lineHeight": 1.5,
    "letterSpacing": 0.32,
    useNextVariants: true,
    suppressDeprecationWarnings: true,
    h6: {
      "fontWeight": 600,
    },
  },
});

<MuiThemeProvider theme={THEME}>
  <Typography variant="h6" color="inherit" align="center" style={{ margin: "1rem" }}>
      "Some text"
  </Typography>
</MuiThemeProvider>

The Box element can help. Ignore the Typography element.

See the code example below

<Box fontWeight="fontWeightLight">…
<Box fontWeight="fontWeightRegular">…
<Box fontWeight="fontWeightMedium">…
<Box fontWeight={500}>…
<Box fontWeight="fontWeightBold">…

See official docs for more details

发布评论

评论列表(0)

  1. 暂无评论