Google fonts in React Native

Asked

Viewed 1,039 times

0

I am trying for some time to add fonts to my React-Native project but did not succeed following tutorials from the net.

I’m trying to use the sources like this:

import React from 'react';
import { View, Text } from 'react-native';

const App = () => {
  return (
    <View>
      <Text
        style={{
          fontFamily: 'LobsterTwo'
        }}
      >App</Text>
    </View>
  );
}

export default App;

I created the folder "Assets" at the root of my project and inside has the fonts directory with downloaded fonts:

inserir a descrição da imagem aqui

My file React-Native.config.js:

module.exports = {
  assets: ['./assets/fonts/'],
};

I ran the React-Native link and started the app, I already called the fonts in all different ways, with strokes, together, separate and still does not work. Thanks if anyone helps, I’m extremely beginner in React Native and I’m lost.

4 answers

3

Consider that you should import the source, not simply use it. You can import either directly from the source site in index.html, or as the file in the CSS folder preferably the project’s global css.

I put down the two forms:

Put in your INDEX.HTML file the import direct from the source site as:

<link href="https://fonts.googleapis.com/css2?family=Roboto:ital,wght@0,100;0,300;0,400;0,500;0,700;1,100;1,300;1,400;1,500&display=swap" rel="stylesheet">

* {
  box-sizing: border-box;
  font-family: 'Roboto', sans-serif;
}
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta name="theme-color" content="#000000" />
    <meta
      name="description"
      content="Web site created using create-react-app"
    />
    <link href="https://fonts.googleapis.com/css2?family=Roboto:ital,wght@0,100;0,300;0,400;0,500;0,700;1,100;1,300;1,400;1,500&display=swap" rel="stylesheet">
    
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.6.0/slick.min.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.6.0/slick-theme.min.css">

    <link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
    <link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
    <title>DevFlix</title>
  </head>
  <body>
    <noscript>You need to enable JavaScript to run this app.</noscript>
    <div id="root"></div>
  </body>
</html>

Another option is to import into the project’s Global CSS as:

  • Remember that to import, consider the full path, name of file (the way it is written) and the extension.

    @font-face { font-family: 'Westline Script Demo'; font-style: normal; font-Weight: Bold; src: url('. /Assets/fonts/westlineScript.otf'); }

import React from 'react';
import { BrowserRouter as Router } from 'react-router-dom';
import "./global.css";

import Main from './main/Main';

function App() {
  return (
    <>
      <Router>
        <Main />
      </Router>
    </>
  );
}

export default App;
@font-face {
  font-family: 'Westline Script Demo';
  font-style: normal;
  font-weight: bold;
  src: url('./assets/fonts/westlineScript.otf');
}
@import url('https://fonts.googleapis.com/css2?family=Inconsolata&display=swap');

* {
  margin: 0;
  padding: 0;
  outline: 0;
  box-sizing: border-box;
  scroll-behavior: smooth;
}

body {
  font: 400 14px "Inconsolata", sans-serif;
  background: #F9DFDE;
  -webkit-font-smoothing: antialiased;
}

span,
p,
h1,
a {
  font: "Inconsolata", sans-serif;
}

button {
  cursor: pointer;
}

1

Hello,

I took a step by step to get you to install and use the custom font.

  1. Download the font you want to use on google fonts, in the example I will use the Poppins.
  2. At the root of the project creates the following structure /assets/fonts.
  3. In the project ra creates the file react-native.config.js with the following content.
module.exports = {
  project: {
    ios: {},
    android: {},
  },
  assets: ['./assets/fonts/'],
};
  1. Executes the command react-native-link, at that time the sources were installed.
  2. Perform the yarn react-native ios or yarn react-native android to reinstall the app.
  3. To use the font, you must use the entire file name .ttf, example for the source Poppins-Regular.ttf, you define fontFamily: 'Poppins-Regular'.

The sources of React-Native, has something a little uncomfortable, as the React-Native does not inherit style, in all texts you need to define, your font family, more this can be bypassed.

Follow the step by step to resolve this.

  1. At the root of the project create the file override.js with the following content.
import React from 'react';
import { Text, TextInput, StyleSheet } from 'react-native';

import theme from './src/global/theme';

const styles = StyleSheet.create({
  defaultText: {
    fontFamily: 'Poppins-Regular'
  },
});

const override = () => {
  const oldTextRender = Text.render;
  Text.render = (...args) => {
    const origin = oldTextRender.call(this, ...args);
    return React.cloneElement(origin, {
      style: [styles.defaultText, origin.props.style],
      allowFontScaling: false,
    });
  };

  const oldTextInputRender = TextInput.render;
  TextInput.render = (...args) => {
    const origin = oldTextInputRender.call(this, ...args);
    return React.cloneElement(origin, {
      style: [styles.defaultText, origin.props.style],
      allowFontScaling: false,
    });
  };
};

export default override;

Should change the Poppins-Regular by his installed source family. 2. In the archive index.js, shall place the following code.

import override from './override';

override();

What you did above was overwrite the standard Text and Textinput style and now every time you put a Text or Textinput they will already have the X font by default.

0

First, Google does not indicate using the font name as you used

The Correct is 'Lobster Two', and not 'LobsterTwo',

Another tip is you create one StyleSheet.create and use the font by adding a class to the text as in this example.

<Text style={styles.fonteGoogle}>
  seu texto
</Text>


const styles = StyleSheet.create({
  fonteGoogle: {
    fontFamily: 'Lobster Two',
  },
})

Maybe this article can help you: https://medium.com/tableless/fontes-customizadas-no-react-native-baa64f1fda6f

0

In fact you have to link the dependencies of your sources to your project.

Inside the folder android/app/src you will create a folder Assets/fonts and inside it you will glue your sources.

So everything will work normal.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.