I am trying to use a font "Verveine Corp Regular' inside my react-native app.
The font works in the iOS build, but not in the Android build.
The font is in .tff format and is placed in the root of my work (linked in the package.json and I have run react-native link) and inside "android/gradle/src/main/assets/fonts" but it's still not picking the font up. I have also cleaned and rebuilt the app multiple times.
When inspecting an element which uses the font in the android debugger, it says it's using the font. But the font is still the default font.
Could anyone offer some help or guidance on this issue?
Thanks!
The other answers helped me get this working for me, thank you. This manuever is probably only necessary when the font has capital letters in the filename. A more complete answer:
Add the font as normal in react native, for example:
{react-native-project}/fonts/GovtAgentBB.ttf
Run react-native link and this will put the font in
{react-native-project}/android/app/src/main/assets/fonts/GovtAgentBB.ttf
{react-native-project}/android/app/src/main/assets/fonts/GovtAgentBB_ital.ttf
But android, bring robotic and not human, doesn't like this. So rename the file with all lower case letters and an underscore before the variant, like:
{react-native-project}/android/app/src/main/assets/fonts/govtagentbb.ttf
{react-native-project}/android/app/src/main/assets/fonts/govtagentbb_ital.ttf
Then, you have to change the font name in the style depending on the platform. For iOS, use the human name that is the name of the font that would be displaying in the title of the window of the Mac Font menu (or just the name you see on the web). For android, you have to, robotically, use the name of the file you just renamed.
{Platform.OS === 'ios' ? (
<Text style={styles.welcome}>
Hello World!
</Text>
) : (
<Text style={styles.welcomeAndroid}>
Hello World
</Text>
)}
const styles = StyleSheet.create({
...
welcome: {
fontFamily: 'Government Agent BB',
},
welcomeAndroid: {
fontFamily: 'govtagentbb',
},
This is how I used custom font in my project
//sampleStyle.js
import overrideStyles from '/sampleoverridestyles';
iconTextStyle: {
color: '#FFFFFF',
fontSize: 16
}
//sampleoverridestyles.ios.js
export default {
iconTextStyle: {
fontFamily: 'FaktSoftPro-Medium'
}
}
//sampleoverridestyles.android.js
export default {
iconTextStyle: {
fontFamily: 'faktsoftpro_medium'
}
}
since I cannot set the font name same for iOS and android I have overridden it as above and it worked.
Guys if you are following all other solution and still no update then try following the steps
add react-native-config.js file
then add fonts in assets/fonts/your font
rename it with lowercase like OpenSans_Regular to opensans_regular
react-native link in terminal in your project folder
after all build your project again using react-native run-android
fontFamily: Platform.OS === "ios" ? 'opensans_regular' : 'cassandrapersonaluseregular_3bjg',
I was also getting the same issue the font was not reflecting changes then I build my project again that was my mistake because we adding fonts in our android folder so we need to compile that again.
I hope someone may save their time
I added font in react-native android from here:
https://medium.com/#gattermeier/custom-fonts-in-react-native-for-android-b8a331a7d2a7#.40vw3ooar
Follow all the steps it will work fine.
After adding run react-native run-android
First, make sure you are up to version 0.16+ with react-native.
Your fonts should be *.ttf or *.otf files and must be located in: /projectname/android/app/src/main/assets/fonts
Make sure the fonts are lowercase only and follow this pattern: fontname.ttf, fontname_bold.ttf, fontname_light.ttf, fontname_bold_italic.ttf
Have you defined another font in your AppTheme (styles.xml), that overrides your preferred font?
Have you tested your font with a "Hello World"-App as a minimal test?
Have you implemented your ttf as shown here for example?: How to use custom font in Android Studio
Related
This question has been already here and here, but the answer is not satisfactory there. The fonts are not working on android but on iOS do. Maybe I made some mistake during the linking.
How did I added fonts:
Created folder assets/fonts/ inside root folder.
Added
"rnpm": { "assets": ["./assets/fonts"] }
react-native link
Console output was it successfully added assets to iOS and Android
Added fontFamily: 'MyCustomFontFamily' into styles.
Run on iOS, everything worked fine.
Run on Android, no typeface.
Checked the android project directory and the assets folder was present
I tried adding (Platform.OS === 'ios') ? 'MyCustomFontFamily' : 'fonts/my_custom_font_family' to go with the file name but this did not work wither.
Tried same variation as 9 without path or with extension but no luck
Read here to use weight, tried it, no luck.
I had the same issue, which had to do with the fact that, as you already know, Android will read from the filename whilst iOS will read from the full name property. And, indeed, I had mixed up my fonts' references.
The first thing I did to solve this was to double-check that my fonts were properly located in android/app/src/main/assets/fonts/.
Then I ended up renaming my fonts to the exact same name as the one used by iOS and using that name as a reference. Here is what I mean:
export const sansSerifLight = 'Aileron-Light'; // Filename is 'Aileron-Light.otf'
export const sansSerifRegular = 'Aileron-Regular'; // Filename is 'Aileron-Regular.otf'
export const sansSerifBold = 'Aileron-Bold'; // Filename is 'Aileron-Bold.otf'
export const serifRegular = 'LibreBaskerville-Regular'; // Filename is 'LibreBaskerville-Regular.ttf'
export const serifBold = 'LibreBaskerville-Bold'; // Filename is 'LibreBaskerville-Bold.ttf'
export const serifItalic = 'LibreBaskerville-Italic'; // Filename is 'LibreBaskerville-Italic.ttf'
export const sansSerifTitle = 'ADAM.CG PRO'; // Filename is 'ADAM.CG PRO.otf'
export const serifTitle = 'Oranienbaum'; // Filename is 'Oranienbaum.ttf
And then :
<MyComponent style={{ fontFamily: serifTitle }} />
This really made things way less error-prone and easier to maintain for me.
I'm currently facing a font weight issue. This occured after I installed a custom font via Xcode. I've linked everything and the custom font works but I still see no effect?
attrName: {
color: '#000',
fontWeight: '300' /* Normally this should work */
},
Has anyone else stumbled upon this issue? Need to solve this fast...
UPDATE
I solved this a while after posting this. If you want different font weights to your text element, make sure to import all types of weight of your font in Xcode or Android Studio. One weight or version of the font is not enough...
I'm facing a similar issue.
react: 16.0.0-alpha.6
react-native: 0.44
fontWeight: '300' works for IOS but doesn't for Android. I certainly don't want to include ALL font weights for every single font I use, that's why I propose the following approach for those who are still looking:
// Style definition
const styles = StyleSheet.create({
base: {
fontFamily: 'Roboto',
},
light: {
...Platform.select({
ios: {
fontWeight: '300',
},
android: {
// RN 0.44.0 bug: fontWeight 300 not linked to *Thin or *Light fonts yet...
fontFamily: 'Roboto-Thin',
},
}),
},
});
Usage example:
<Text style={styles.base}>Hello world</Text>
<Text style={[styles.base, styles.light]}>Hello light world</Text>
In future releases, this will most likely be fixed by the API, but the devs have got their hands full for now :)
I was facing the same issue, but the I realized that is should be -
fontWeight:'bold',
not -
fontweight:'bold',
the only difference is w->W
ya some time we get wrong auto suggestion, usually happens a lot when you work on lot of different languages.
I am trying to use icon font FontAwesome in Nativescript application, which is possible according to this article https://www.nativescript.org/blog/mobile-app-best-practices---use-font-instead-of-image-to-show-an-icon
I did everything that is described in that article:
Added .ttf in app/fonts
Added class in app.css
.fa{
font-family: "FontAwesome";
}
Used it in XML like so
text="" class="fa"
But result is rather disappointing:
I also tried the "\uf230" syntax, but that renders as plain text.
What am I doing wrong?
Could be a few things. Does it work on iOS? As your CSS definition is iOS compatible, not Android as Android needs the actual filename (without extension) whereas iOS needs the font name. So I have this to be xplatform-ready (the file is 'fontawesome-webfont.ttf'):
.fa {
font-family: 'FontAwesome', fontawesome-webfont;
}
The \f005 syntax is OK (<Label class="fa" [text]="'\uf005'"></Label>), but I'm using the splendid nativescript-ngx-fonticon plugin (there's also a non-Angular one) to be able to do this instead:
<Label class="fa" [text]="'fa-shopping-basket' | fonticon"></Label>
To make it work, you must make sure that the "fonts" directory is inside the "app" folder and that the following files exist:
font-awesome.eot
font-awesome.ttf
I opted to adopt this font as the default of my application, so I do not have to worry about where I'm going to use it and how much to enter the right class, everything is getting very good and the result is perfect.
In CSS, you only have to define a selector according to your interest for the source to be used, so just use the directive:
page {
font-family: 'FontAwesome'
}
Then where you want an icon, just use an html entity that represents it as it searches the site: http://fontawesome.io/icons/
See images:
You can see this video where I was based to start. It corrects in the video the extension used to be attentive.
I couldn't manage to change the style of TextInput in React Native when the secureTextEntry prop set to true. Also the default style of input changes, I understood this is due to android native behaviour. The below code does not change the style unless I unset secureTextEntry.
<TextInput secureTextEntry={true} style={{color: "white"}}/>
I have a login form and having different styles for email input and password input is disturbing. Does anyone know how to fix this?
UPDATED
Seems like the following PR will fix the issue:
PR Link
<TextInput
placeholder={'Environment'}
placeholderTextColor="#202020"
secureTextEntry={true}
/>
This PR has now been merged to fix it, and it should probably land in 0.23.
If like me, you don't want to wait for it, or can't upgrade easily, you can use this module I created, as described in this blog post.
Basically, install the module with:
npm install --save react-native-text-input
Link it to your native code: I strongly recommend the use of RNPM to link native modules:
npm install -g rnpm
rnpm link react-native-text-input
Now you can replace:
import { TextInput } from 'react-native';
by:
import TextInput from 'react-native-text-input';
And tadaaaaa!
Your input is now white :)
The TextInput from this module is actually a copy-paste from the default React Native one, with the fix in the PR above added.
It's a known RN issue. You can track it here. Unless you submit a PR, I think we'll have to wait for that feature to come.
Question 1:
I add a fontFamily to index.android.js's welcome style, but it takes no effect. Does fontFamily actually work on android?
welcome:{
fontSize:20,
fontFamily:'roboto-thin',
textAlign:'center',
margin:10}
Question 2:
if fontFamily works on android, is there a way to load custom font from assets? Or is this some feature to be implemented by react-native?
For Android:
Custom fonts were added with 0.16.0-rc. So you need to have 0.16.0-rc version first and after that you can just download any fonts from the web.
Put your font files in projectfolder/android/app/src/main/assets/fonts/font_name.ttf
Remember to recompile which is: react-native run-android
And then you can use fontFamily: 'font_name' in your style.
Also note the following restrictions for custom Android fonts in react-native:
fonts must be placed in android/app/src/main/assets/fonts
only .ttf files are supported
The name of the file has to match the fontFamily exactly. For instance, if fontFamily is 'Source Sans Pro',
the file must be called Source Sans Pro.ttf (and NOT SourceSansPro.ttf). Any suffixes as mentioned in the following paragraph are automatically removed from the file.
When the font is in bold and/or italic, it must use on the following suffixes:
_bold (e.g. Source Sans Pro_bold.ttf)
_italic
_bold_italic
Hello this issue waist for me more than two days with "El+Messiri" font "https://fonts.google.com/specimen/El+Messiri"
i was doing every think write :
Put your font files in
projectfolder/android/app/src/main/assets/fonts/ElMessiri-Regular.ttf
Remember to recompile which is: react-native run-android And then
you can use fontFamily: 'ElMessiri-Regular' in your style.
but the fault was that am using fontWeight : 'bold' after fontFamily: 'ElMessiri-Regular' and the fontWeight was overiding the fontFamily because "El+Messiri" font has his own fontFamily bold wich is "ElMessiri-Bold"
I believe the following is a cleaner alternative to the methods already explained here:
Put all your fonts in you React-Native project directory
./assets/fonts/
Add the following line in your package.json
"rnpm": {
"assets": ["./assets/fonts"]
}
finally run in the terminal from your project directory
$ react-native link
to use it declare this way in your styles
fontFamily: 'your-font-name without extension'
If your font is Raleway-Bold.ttf then,
fontFamily: 'Raleway-Bold'
Source https://medium.com/#danielskripnik/how-to-add-and-remove-custom-fonts-in-react-native-b2830084b0e4
This feature has yet to be implemented. See the relevant issue on github here.
It seems like custom font was added, check out this commit:
https://github.com/facebook/react-native/commit/bfeaa6a4f531cfc18c097bc9ffb6a8dbe3ddc702
For those facing problems with iOS only - which sometimes does not recognize the fontFamily name correctly:
The only solution that solved my problem was to log all the fonts to find out correct naming
(make sure you do the steps below only after adding the assets/fonts and running the react-native link):
Anyway, to log them, open the appName.xcworkspace file with xcode and then edit AppDelegate.m putting this lines at the end of the didFinishLaunchingWithOptions method (and before the return statement):
for (NSString* family in [UIFont familyNames])
{
NSLog(#"%#", family);
for (NSString* name in [UIFont fontNamesForFamilyName: family])
{
NSLog(#" %#", name);
}
}
So, when you run the app (from xcode) it outputs something like this:
This way, I can use the fontFamily Barow-Light or Zapfino inside my react-native styles
Hope it helped!
I had an incredibly hard time with this, even after following all advice I found.
What worked for me: ensure the font file is named using only lowercase letters and underscores. Re-link. Done.
A lot of hair pulling has been done over this yet it isn't mentioned anywhere as far as I can see.