Expo / React-native change fonts between Android and IOS - react-native

I want to know how to have different fonts for IOS and Android,
I have only 1 index file.(App.js)
My code that works on IOS:
<Text style={{fontFamily: 'AppleSDGothicNeo-Thin'}}>Hello!</Text>
But when I open the App on Android I see the standard font (Arial?)
I tried something like this:
<Text style={{fontFamily:'Roboto', fontFamily:'AppleSDGothicNeo-Thin'>Hello!</Text>
But this just gives me an Error that the font wasn't found.

You can use condition in your style using Platform component from React Native
import { Platform } from 'react-native';
<Text style={{fontFamily: (Platform.OS === 'ios') ? 'AppleSDGothicNeo-Thin' : 'Roboto'}}>Hello!</Text>
Also be sure fonts are well imported.
Otherwise import them with the following steps.
1 - Place the fonts you want to use in a directory inside your project. For example in ./assets/fonts/
2 - Add the following line in your package.json:
“rnpm”: {
“assets”: [“./assets/fonts”]
}
3 - run in terminal:
$ react-native link

Related

React native Custom font is not working in android

I have followed all the procedures to add and use a custom font in my react native app but it is not working. Below is what I have followed.
Added assets/fonts under the root directory and added ttf fonts to it
Pic here
Created react-native.config.js and added the below code
module.exports = {
assets : ['./assets/fonts']
}
some sources said to add the below in package.json. Added it as well
"rnpm": {
"assets": ["./assets/fonts"]
}
Now, inside my component I am exactly using the same ttf name for font family style.
style= {styles.textStyles}
const styles = StyleSheet.create({
textStyles : {
fontFamily : 'Good Feeling Sans',
fontSize : 20,
fontWeight : 'bold',
margin : 10
} })
Ran "npx react-native link" and it was linked successfully
Linked Pic
7.Ran "npx react-native run-android" and I see no change in the font. :( Any idea why it does not reflect
Try to declare your font style without fontWeight parameter, so please try to declare your font style as:
const styles = StyleSheet.create({
textStyles : {
fontFamily : 'Good Feeling Sans',
fontSize : 20,
margin : 10
}
})
In stead of manipulating with fontWeight, you can use special prepared fonts for this (ttf files with weight variants). But if you really need the fontWeight you can try to declare it with number value, like in example: fontWeight:800
It was a problem with fontWeight I noticed in previous versions of React Native (< 0.60), maybe still present nowadays.
Make sure you also placed your font files in proper folder, accessible to your app. In your pic, assets/fonts is misplaced I believe, it should be placed under android/app/src/main/assets/fonts folder.
Just go to android folder and create android/app/src/main/assets/fonts
in that folder just drop your custom fonts.
Try this:
Rename the font file name to lowercase & Remove - from file name.
Run npx react-native link
Restart app npx react-native run-android

Check if device is jailbroken/rooted using Jail Monkey in React Native fails module is 'undefined'

In React Native I found two plugins to check if a device (iOS/Android) is jailbroken/rooted:
Jail Monkey
react-native-is-device-rooted
I have firstly tried the npm package react-native-is-device-rooted but it doesn't work and it seems to be outdated. So I tried Jail Monkey, but I get this following error:
The code is:
import JailMonkey from 'jail-monkey'
export default class Welcome extends Component {
render() {
return (
...
<View style={styles.lowerView}>
<CustomButton text={"Jail Monkey"} onPress={() => this.printJailMonkey()}/>
</View>
...
);
}
printJailMonkey = () => {
console.log("Jail Monkey library content: " + JailMonkey.isJailBroken())
}
}
I have checked carefully the manual link of the package (using Xcode, pod install, and so on...). Nothing worked, does someone can help me?
JailMonkey uses Native Modules and thus cannot run in an Expo managed app. You need to eject it to ExpoKit for JailMonkey to work.
Solved but doing manually the linking.
Try following steps for manual linking.
Navigate to root folder in terminal enter commands:
react-native link
cd ios
pod update

Problem when rendering Open-sans font in React Native V 60+

i want to add 'Open-sans' font to my App but i have an issue and it happens on IOS simulator only
here is my steps :
a) I've created a folder in root app as : assets then fonts fonts folder in assets and put Open-sans.ttf
b) I've made a file in root with the name react-native-config.js and add:
module.exports = {
project: {
ios: {},
android: {},
},
assets: ['./assets/fonts/'],
};
c) react-native link
d) In the App.js
<Text style={{fontFamily: 'OpenSans-Bold', fontWeight: 'bold'}}>
This is my first App
</Text>
My problem is it keeps telling me `Unrecognized font family' whenever i made any edit in the App.js
but when i open react-native-config.js and do nothing but save only the hot reload fired and the font is working
as i mentioned it's perfect on Android but the error appears on IOS when save after any edit on App.js file
For IOS the font-family value is fullname and for android its file name.
Try:
<Text style={{fontFamily: 'Open Sans Bold'}}>This is my first App</Text>
For Android, change the file name 'OpenSans-Bold.ttf' to 'Open Sans Bold.ttf'
I was with the same error, so thats what I did:
1 - Rename react-native-config.js to react-native.config.js and put this script on file:
module.exports = {
assets: ['./src/assets/fonts'], // this is my path
};
2 - put it on my package.json :
rnpm": {
"ios": {},
"android": {},
"assets": [
"./src/assets/font"// this is my path
]
},
and After I run react-native link && yarn react-native link.
Check if your info-plist and your android assets are changed
As per your description you have only put Open-sans.ttf in asset folder. But you are trying to access OpenSans-Bold which is not added to your asset folder. To get access to any particular font, you need to add that one to the asset folder. Here is an example for your referrence...
you can check this too for better understanding

Which is the best approach to insert an vector (svg) graph into a react native application [duplicate]

This question already has answers here:
SVG use in React Native project
(4 answers)
Closed 4 years ago.
I need to insert a vector (.svg) image into a react-native app. Could you help me please.
I have no idea how to achieve this, and seems like react-native doen't support svg.
Any help will be appreciated.
There are some tools (https://github.com/seekshiva/react-native-remote-svg and others), however, I tried some these tools without good results, so I decided to create a vector font and that works very well. For that you can convert a set of svg images to font (.ttf) using these techniques. You will really get more customizable options with a font instead an image.
I choosed the IcoMoon to create the font (you also can do it with http://fontello.com/ or any other solution you prefer), after create your font, proced the download, you will get a .zip file contain the selection.json and the icomoon.ttf, copy both files (place the files as showed below) to your RN project. The next step is install react-native-vector-icons, with vector-icons you also can work with free icons fonts as font-awesome and others. GitHub: https://github.com/oblador/react-native-vector-icons
Install the dependencies and link it to ios an android folders with:
npm install react-native-vector-icons --save
react-native link
sample using fontawesome:
import Icon from 'react-native-vector-icons/FontAwesome';
const myIcon = (<Icon name="rocket" size={30} color="#900" />)
Put your icons in your app. To use the custom font you created, you have to:
Put your .ttf in a ./resources/fonts folder at the base of your project
Add this piece of code at the first level of your package.json:
"rnpm": { "assets": [ "resources/fonts" ] },
In your terminal: react-native link
And to use your custom font in a react native you have to:
import React, { Component } from 'react';
import {Platform, View } from 'react-native';
import { createIconSetFromIcoMoon } from 'react-native-vector-icons';
import icoMoonConfig from './selection.json';
const Icon = createIconSetFromIcoMoon(icoMoonConfig);
//a constant with your icon with with and color
const home = (<Icon name="home" size={30} color="#f7a805" />);
export default class App extends Component<Props> {
render() {
return (
<View style={styles.container}>
{home}
</View>
);
}
}

Where to store images in react native for Android and iOS?

I'm new to react native and want to build a small test app in Android and iOS.
I have created an images directory alongside my index.ios.js and index.android.js files.
My code below produces a red screen error saying "unable to resolve module ./images/tree.png... Invalid directory...":
'use strict';
var React = require('react-native');
var {
AppRegistry,
StyleSheet,
Text,
View,
Image,
} = React;
var TestApp = React.createClass({
render: function() {
return (
<View>
<Text>test app</Text>
<Image
style={styles.thumbnail}
source={require('./images/tree.png')} /> />
</View>
);
},
});
var styles = StyleSheet.create({
thumbnail: {
width: 100,
height: 100,
},
});
AppRegistry.registerComponent('TestApp', () => TestApp);
Where should I place my images and how should I reference them?
Your code looks correct assuming that the images directory is in the same location as the index.*.js files.
What version of React Native do you have? From the Images doc page:
Available React Native 0.14+. If you've generated your project with
0.13 or earlier, read this. The new asset system relies on build hooks for Xcode and Gradle that are included in new projects generated with
react-native init. If you generated your projects before that, you'll
have to manually add them to your projects to use the new images asset
system. See Upgrading for instructions on how to do this.
If you want to verify your react-native version, the easy way to do this is to run the following npm command from your project directory:
npm ls react-native
The current release version is 0.15.0.
Make sure you don't forget to run react-native upgrade after updating the package as this is required for adding build hooks for the new asset system to existing projects. If you previously updated your react-native version without doing this, that may explain the issue you are having.
If you happen to be using Windows for development, see my answer to this question for details on how to work around a bug with the new asset system on Windows.