search

Home  >  Q&A  >  body text

Custom fonts are not rendered in RN-SVG <Text/>

<p>In my code I try: </p> <pre class="brush:php;toolbar:false;">import { useFonts } from 'expo-font' useFonts({ 'Robo Numbers':require( '../assets/fonts/my-custom.ttf' ), } )</pre> <p>Now when I render RN's own or RN-Paper's <code>Text</code> element, the font family displays correctly: </p> <pre class="brush:php;toolbar:false;">import { Text } from 'react-native-paper' const styles = StyleSheet.create({ countdown:{ fontFamily:'Robo Numbers', fontSize:34, }, }) <Text style={[ styles.countdown, { color:10 > countdown ? 'orange' : '#f8f0c1' } ]}>{countdown}</Text></pre> <p>But rendering the default font in RN-SVG's <code>Text</code>: </p> <pre class="brush:php;toolbar:false;"><Text stroke={10 > countdown ? 'orange' : '#f8f0c1'} fontFamily="Robo Numbers">{countdown}< ;/Text></pre> <p>Is this functionality missing from the library, or am I missing something? </p>
P粉743288436P粉743288436454 days ago539

reply all(1)I'll reply

  • P粉402806175

    P粉4028061752023-09-07 00:20:57

    So far, "react-native-svg": "~13.4.0" Custom fonts are not supported, or I can't find a documented way to use them.

    So I found a very simple solution based on :

    render() {
      const { countdown } = this.state
    
      return <>
        <ForeignObject x={85} y={94} height={14} key={countdown}>
          <Text style={{ fontFamily:'Robo Numbers' }}>{countdown}</Text>
        </ForeignObject>
      <>
    }

    The CRITICAL here is the ForeignObject@key attribute. It must be present to force the ForeignObject to re-render its children.

    In my case I increment the countdown variable in setInterval() but the text never updates until I add the ForeignObject@key with the changing value !

    Hope this helps someone...

    reply
    0
  • Cancelreply