Support for Custom Fonts
See original GitHub issueI 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.
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:
- Created 5 years ago
- Comments:8 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I add the @import font from Google Fonts to index.html.js style css.
Hi! Any updates? What about API for loading custom fonts?