Import font into React application

六眼飞鱼酱① 提交于 2021-02-07 11:55:14

问题


I'm trying to use the Roboto font in my app and having tough time..

I did npm install --save typeface-roboto and added import 'typeface-roboto' to my React component. But still can't get my font to change.

I am trying to do like this :

const React = require('react')
import 'typeface-roboto'

class Example extends React.Component {

  render() {

    let styles = {
      root: {
        fontFamily: 'Roboto'
      }
    }

    return (
      <div style={styles.root}>
        <p>
          This text is still not Roboto ?!???!!1
        </p>
      </div>
    )
  }
}
module.exports = Example

Am I missing something? Looking for a simple way to do this without any external css file..


回答1:


Try adding import 'typeface-roboto'; in root file i.e. index.js file.

In my case I added font-family to body element and it is worked.




回答2:


import this code of line in your main css or scss whatever use use

@import url('https://fonts.googleapis.com/css?family=Roboto:300,400,500&display=swap');

this will work. If you want to customize this then go to the google font and select font style font weight whatever you want. Here i have selected font weight 400,300 and 700 If have not selected the font weight then you can not use other font weight




回答3:


I had the same issue, couldn't get Roboto fonts for my components anyhow.

I used the webfontloader package.

yarn add webfontloader

or

npm install webfontloader --save 

h Once you do this, add the following code to your index.js or to the JS file which is an entry point to your application

import WebFont from 'webfontloader';

WebFont.load({
  google: {
    families: ['Titillium Web:300,400,700', 'sans-serif']
  }
});
// rest of the index.js code

And now apply the font-family.




回答4:


It involves several changes, assuming you want to use CSSinJS:

  1. change style to className
  2. You'll need some kind of library to apply styles.

With react-jss

import React from 'react';
import 'typeface-roboto';
import injectSheet from 'react-jss';

const styles = {
  root: {
    fontFamily: 'Roboto',
  },
};

class Example extends React.Component {
  render() {
    return (
      <div className={styles.root}>
        This text is Roboto
      </div>
    )

  }
}

const StyledExample = injectSheet(styles)(Example)

export default StyledExample;

Or with MaterialUI styles:

import React from 'react';
import 'typeface-roboto';
import { withStyles } from '@material-ui/core/styles';

const styles = theme => ({
  root: {
    fontFamily: 'Roboto',
  },
});

function Example(props) {
  const { classes } = props;

  return (
    <div className={classes.root}>
      This text is Roboto
    </div>
  );
}

export default withStyles(styles)(Example);

Or, you could just do it the right way, via Dan's Answer

Or the Styled-Components way:

import styled from 'styled-components'

const ExampleStyled = styled.div`
  @font-face {
    font-family: 'Roboto';
    src: local('Roboto'), url(fonts/Roboto.woff) format('woff');
  }
`

const Example = () => (
  <ExampleStyled>
     This text is Roboto!
  </ExampleStyled>
);

export default Example;



回答5:


You simply just require('typeface-roboto') it.

const React = require('react')
require('typeface-roboto')

class Example extends React.Component {

  render() {

    let styles = {
      root: {
        fontFamily: 'Roboto'
      }
    }

    return (
      <div style={styles.root}>
        <p>
          This is now Roboto
        </p>
      </div>
    )
  }
}
// or here instead if you fancy
    .root {
        font-family: 'Roboto';
    }


来源:https://stackoverflow.com/questions/49204511/import-font-into-react-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!