Can't set custom font in WebView React Native
See original GitHub issueIs this a bug report?
No
React Native 0.51 Testing On Android OS: Ubuntu 16.04 GNOME
I have already asked this on StackOverflow but nobody has answered it. I’ve been searching and trying different things for 1 week now. I’m using a WebView
to show some text with proper justification (Android doesn’t support justification). I’m exhausted trying to apply my custom (local) font in WebView
text style. I’ve tried the code below but no result:
var html = `<h1 style="font-family: MyFont"> some text here </h1>`
<WebView
source={{html}}
style={{width: 200, height: 200}}
/>
This is my react native file structure:
Project
-android
-node_modules
-...
-assets
--fonts
---MyFont.ttf
MyFont.ttf
is located inside Project/assets/fonts/
. I tried this too but can’t get it to work. it says i should set the baseUrl but i don’t know what url to set. Please help me I really need a comprehensive and working example.
Issue Analytics
- State:
- Created 6 years ago
- Comments:10
Top Results From Across the Web
How to set custom fonts in WebView(react-native-webview) in ...
Set the following font URL based on platform: const fontUrl = Platform.select({ ios: "Poppins-Bold.ttf", ...
Read more >[iOS]: Custom Font not working · Issue #1016 · react-native ...
On iOS, custom fonts are not used within the WebView. I followed the instructions on a few prior bug reports but I can't...
Read more >Ultimate guide to use custom fonts in react native - Medium
Adding custom fonts in react native project is very simple task with react-native link command but can be tricky if you are a...
Read more >[Solved]-How to set custom fonts in WebView(react-native ...
Coding example for the question How to set custom fonts in WebView(react-native-webview) in iOS?
Read more >Blog: Fix Custom Font Inconsistency in React Native - Vincit
If your custom fonts are starting to drive you mad in your react-native projects, you're not alone! Here's how to fix them.
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 Free
Top 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
Tried this on Andorid, not working 😦 @fahadhaq
I have not tried this yet. But I had this code working on my Android app previously. Currently its being re-written in React-native.
So from what I understand you need to set the baseUrl in the html.
This is what I had previously in my android code for HTML
As you can see I added a link to the font in the HTML.
My android code however had no BaseUrl defined. I assume it was reading the baseurl from the HTML here is the line from android that loaded the data
webViewDeals.loadDataWithBaseURL(null, wbStr, "text/html", "UTF-8", null);
loadDataWithBaseURL the first param is baseurl. As you can see I have left it empty. but if you define the base url you prob wont need to set it in the HTML.
Hope this helps you. I am working on this later today. Will let you know if this works for me. I am trying to figure out how to link to iOS.
Thanks in advance