[iOS]: Custom Font not working
See original GitHub issueBug description: On iOS, custom fonts are not used within the WebView. I followed the instructions on a few prior bug reports but I can’t get the custom font running on iOS.
To Reproduce:
To generate the html for a WebView, I created this function
export const generateHtml = (content, textColor, fontFileName = 'NeuzeitOffice-Regular', fileFormat = 'ttf') => {
const fileUri = Platform.select({
ios: `${fontFileName}.${fileFormat}`,
android: `file:///android_asset/fonts/${fontFileName}.${fileFormat}`
})
return (
`
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<style media="screen" type="text/css">
html, body {
height: 100%;
width: 100%;
margin: 0;
background-color: transparent;
@charset "UTF-8";
@font-face {
font-family: ${fontFileName};
src: url('${fileUri}'), local('${fontFileName}'), format('${fileFormat === 'ttf' ? 'truetype' : 'opentype'}');
};
font-size: 15px;
line-height: 22px;
text-align: left;
color: ${textColor};
}
</style>
</head>
<body>
${content}
</body>
`
)
}
I use it like this:
import { ActivityIndicator, View } from 'react-native'
import { WebView } from 'react-native-webview'
const html = generateHtml(content, '#FFF')
<WebView
pointerEvents='none'
scrollEnabled={false}
source={{ html }}
renderLoading={() => <View style={{ flex: 1 }} ><ActivityIndicator size='small' color='#FFF' /></View>}
startInLoadingState
allowFileAccess
allowUniversalAccessFromFileURLs
allowingReadAccessToURL='file:///assets/fonts'
/>
I tried to use the last 3 properties to get using the custom font on iOS running but it didn’t help.
I also tried it on a real Device - not only on the Simulator.
Expected behavior:
As this code works perfectly fine on Android, I would like to know what I have to do to make it run on iOS too. It would be great to include an example in the documentation. I’d like to contribute it to the documentation if I learn how to do it.
Environment:
- OS: iOS
- OS version: iOS 13.0 and iOS 13.2
- react-native version: 0.61.2
- react-native-webview version: 7.4.2
Issue Analytics
- State:
- Created 4 years ago
- Reactions:7
- Comments:51 (3 by maintainers)
Top Results From Across the Web
Custom Font in ios not working - Stack Overflow
Another thing to check is on a Mac: Open the app Font Book. Then go file -> file validation ...
Read more >Common Mistakes With Adding Custom Fonts to Your iOS App
Common Mistakes With Adding Custom Fonts to Your iOS App · 1. Include your fonts in your XCode project · 2. Make sure...
Read more >iOS 14.x Custom Font Issue | Apple Developer Forums
We have noticed an issue on 14.x related to loading the fonts mentioned in our CSS. The font file is not getting loaded...
Read more >Custom Fonts (webfonts) not displaying on Safari and iOS
This is the code I am using for custom font, which is working fine on some browsers but is replaced by a serif...
Read more >How to add custom fonts to iOS app - Sarunw
Adding a font to the project is not enough. You need to introduce them to iOS. To do this, click on Info.plist and...
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
Here is a Hello World app with custom font in
react-native-webview
that works both on Android and iOS. Hope this will help.https://github.com/mohammad-haque/react-native-webview-custom-font
I have a solution, you guys can use this tool https://transfonter.org to transform the font to base64 and add the generated css to html, I have tested and it working https://imgur.com/MMhMrQg