question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support for Custom Fonts

See original GitHub issue

I am trying to add a custom font to the canvas and it does not work, the custom font works when I use it on a Text component.

issue

you can run it from here: https://snack.expo.io/@antonlopez/custom-fonts

and here is my code:

import React from 'react';
import { StyleSheet, Text, View, Dimensions } from 'react-native';
import { Font } from 'expo';
import Canvas, {Image as CanvasImage} from 'react-native-canvas';
const SCREEN_HEIGHT = Dimensions.get('window').height;
const SCREEN_WIDTH = Dimensions.get('window').width;



export default class App extends React.Component {
  constructor(props) {
  super(props);
  this.state = {
    loadingFont: true
  };
}

async componentWillMount() {
    await Font.loadAsync({
     'Biysk': require('./assets/fonts/Biysk.ttf'),
    });
    this.setState({ loadingFont: false });
  }

handleCanvas = (canvas) => {
  
   const image = new CanvasImage(canvas);
   canvas.width = SCREEN_WIDTH;
   canvas.height = SCREEN_HEIGHT + 20;

   const ctx = canvas.getContext('2d');

   image.src = 'https://image.freepik.com/free-vector/unicorn-background-design_1324-79.jpg';
   image.addEventListener('load', () => {
     ctx.drawImage(image, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
     ctx.fillStyle = 'purple';
     ctx.textAlign="center";
     const customFont = `${this.state.loadingFont ? 'Verdana': 'Biysk'}`;
     console.log(customFont) // verified that the custom font is loaded
     ctx.font = `44px ${customFont}`;
     ctx.fillText('This is a test',200,80);
   });
  }


  render() {
    return (
      <View style={styles.container}>
        <Canvas ref={this.handleCanvas}/>
        <Text style={{ fontFamily: `${this.state.loadingFont ?  '': 'Biysk'}`}}>
            Custom Font style works
        </Text>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  }
});

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
erickcruscommented, Sep 7, 2022

I add the @import font from Google Fonts to index.html.js style css.

...
<meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scaleable=no" name="viewport">
    <style>
      @import url('https://fonts.googleapis.com/css2?family=FONTHERE');
      html {
        -ms-content-zooming: none;
        -ms-touch-action: pan-x pan-y;
      }
...
1reaction
Tanatcucommented, Feb 8, 2019

Hi! Any updates? What about API for loading custom fonts?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Web Fonts: How to Make Them Work Perfectly in Email - Litmus
Get your complete guide to web fonts in email and learn about email client support, where to find web fonts, choosing a fallback,...
Read more >
Using custom fonts - Studio Help - Google Support
Using custom fonts. You can include custom fonts in HTML5 creatives by uploading a custom font file to Studio and using the CSS...
Read more >
The Easy Way to Add Fonts to Your Website (Including ...
What fonts can be used on the Web? · Different font formats and browser support · How to add custom fonts using @font-face...
Read more >
How to add custom fonts in email templates (classic editor)
You will learn · Custom font support and limitations · Best practice: select a fallback font · Add a Google Font to your...
Read more >
Download and install custom fonts to use with Office
Install a custom font you have downloaded · Find the font file you downloaded - it likely has a .ttf or .otf extension...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found