5

I am using react-native webview to display some web content in my react-native app. and my app font-family and webview font-family is different. I want to change webview font-family from app.

Is there any method to change font-family of webview (custom-font-from-assets-directory), please help me.. Thanks

jamal
  • 1,007
  • 1
  • 11
  • 26

1 Answers1

4

you can achieve this, by injecting java script in-to your web-view

i.e.

injecting following code into web view which will change body tag fontFamily

const INJECTED_JAVASCRIPT = `(function() {
   let body = document.getElementsByTagName("BODY")[0];
  body.style.fontFamily = "Courier New";
})();`;

Web View Componenet

<WebView
        source={{ uri: 'https://reactnative.dev' }}
        onLoadEnd={()=>console.log('Successfuly Loaded')}
        injectedJavaScript={INJECTED_JAVASCRIPT}
        onError={syntheticEvent => {
          const { nativeEvent } = syntheticEvent;
          console.warn('WebView error: ', nativeEvent);
        }}
      />

Working Example Link

Muhammad Iqbal
  • 1,394
  • 1
  • 14
  • 34