Custom fonts are not rendered in RN-SVG <Text/>
P粉743288436
P粉743288436 2023-09-06 09:01:24
0
1
543
<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粉743288436
P粉743288436

reply all(1)
P粉402806175

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 ! p>

Hope this helps someone...

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template