fontFamily unrecognized in react native ios app - reactjs

I'm struggling to get my font, PressStart2P, loaded on. Its a font from google fonts. . . Have gone through the following steps, multiple times, to make sure I wasnt sloppy. By all indications, the font should be available in the react native app.
created the assets folder at the root of my project directory
create the fonts folder within the assets folder and place the font files inside... exactly as the PostScript name indicates, and have used both .otf and .ttf versions of the font.
in package.json, specified where the custom fonts are located:
"rnpm": {
"assets": [
"./assets/fonts/"
]
},
in my terminal, entered npx react-native link
added my fonts in App.js
For reference, here is the entire App.js file:
import React, { Component } from 'react';
import { Text, View , TouchableOpacity} from 'react-native';
export default class HelloWorldApp extends Component {
render() {
return (
<View style={{ backgroundColor:'#e9ffc945',flex: 1,
justifyContent: "center", alignItems: "center" }}>
<Text style={{fontSize:26, color: "#85321b45",
fontFamily: "PressStart2P"}}>Allah'u'Abha :)</Text>
<TouchableOpacity
style={{
backgroundColor:'#a4a6fc19',
padding: 95,
borderRadius: 19,
}}
>
<Text
style={{
color: "white",
fontSize: 20
}}
>
</Text>
</TouchableOpacity>
</View>
);
}
}
Additionally, I've checked in my copy bundle resources, in my project target in xcode... the file is there. Any ideas out there ?

I think you are using Font Family name which is not recognized with iOS. Instead of Font Family name, you should use PostScript Name of each font
Open Font Book ( Spotlight type FontBook )
Select your Font and view more information and it will look like this
You need to give PostScript name of the font as fontFamily . Otherwise android will work but iOS will give errors.
This is updated codebase of your App.js
import React, { Component } from 'react';
import { Text, View , TouchableOpacity} from 'react-native';
export default class HelloWorldApp extends Component {
render() {
return (
<View style={{ backgroundColor:'#e9ffc945',flex: 1,
justifyContent: "center", alignItems: "center" }}>
<Text style={{fontSize:26, color: "#85321b45",
fontFamily: "PressStart2P-Regular"}}>Allah'u'Abha :)</Text>
<TouchableOpacity
style={{
backgroundColor:'#a4a6fc19',
padding: 95,
borderRadius: 19,
}}
>
<Text
style={{
color: "white",
fontSize: 20
}}
>
</Text>
</TouchableOpacity>
</View>
);
}
}
After that link your assets again and clean project using xcode and rebuild.
Now you are ready to go...
Make sure you have link your assets and clean project before running

The recommended way is to create your own component, such as MyAppText. MyAppText would be a simple component that renders a Text component using your universal style and can pass through other props, etc.
https://facebook.github.io/react-native/docs/text.html#limited-style-inheritance

Related

In NativeBase, how can I avoid inline styling props of component and write it separately and then pass it

I am just trying to write a clean code and avoid lots thing in same tag. For example: Text and Box component have could have so many styling props, So is there any way I can write it separately and they pass it as a whole object as style.
import { Box, extendTheme, NativeBaseProvider, Progress, Text, View } from 'native-base'
import React, { FC } from 'react'
const TrainingList: FC = () => {
return (
<NativeBaseProvider>
<Box my="2">
<Text fontSize="16" lineHeight="21.8" bold> Annual Training </Text>
<View my="2" >
<Text fontSize="14" lineHeight="19.8"> Due in 2 days (01/12/22) </Text>
<Text fontSize="14" lineHeight="19.8"> 50% complete / 10 hrs left </Text>
</View>
<Progress rounded="12" colorScheme="warning" bg="#D7D7D7" size="sm" value={65} mx={0} />
</Box>
<Box mt="5">
This is Box2
</Box>
</NativeBaseProvider>
)
}
export default TrainingList
In my eyes, the best approach will be to add variants to the NativeBase Text and Box component. It'll keep your code but also you'll be able to share consistent style throughout the codebase.
And if you wanna know HOW? I'm sharing a few resources to follow.
Resource:
Official Docs: First place to go.
Snack: A demo snack to show how to add variants.
Blog: To better understand how to effectively customise NativeBase components.
You can use StyleSheet for that. You can use what you defined under styles on components.
import React from "react";
import { StyleSheet, Text, View } from "react-native";
const App = () => (
<View style={styles.container}>
<Text style={styles.title}>React Native</Text>
</View>
);
const styles = StyleSheet.create({
container: {
flex: 1,
padding: 24,
backgroundColor: "#eaeaea"
},
title: {
marginTop: 16,
paddingVertical: 8,
borderWidth: 4,
borderColor: "#20232a",
borderRadius: 6,
backgroundColor: "#61dafb",
color: "#20232a",
textAlign: "center",
fontSize: 30,
fontWeight: "bold"
}
});
export default App;
Yes, that's possible using styled components. You can accept props inside of your styling. Alternatively, I prefer using stylesheets as it's simpler and still keeps your code clean. Ex:
<Box style={styles.boxMain}>
<Text style={styles.textOneStyle}> Annual Training </Text>
<View my="2">
<Text style={styles.textTwoStyle}> Due in 2 days (01/12/22) </Text>
<Text style={styles.textTwoStyle}> 50% complete / 10 hrs left </Text>
</View>
</Box>
);
};
const styles = StyleSheet.create({
textOneStyle: {
fontSize: 16,
lineHeight: 21.8,
fontWeight: "bold"
},
textTwoStyle: {
fontSize: 14,
lineHeight: 19.8,
},
});

does View in react native work same as Div in HTML?

I want to render multiple element with different styles but View and child View is not working as div working in html. please guide or share some resources to learn how multiple View work in a return.
import React from 'react'
import { Button, StyleSheet, Text, View,ScrollView } from 'react-native';
export default function Home() {
return (
<View>
<View style={styles.container}>
<Text >
Books are the best Mentors
</Text>
</View>
<View>
<Text style={{backgroundColor:'red'}} >
Books are the best Mentors
</Text>
</View>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
container2: {
flex: 1,
backgroundColor: '#fff',
position:'absolute',
bottom:0
},
});
TL;DR <div> is not the same as <View>
It looks like the possible issue here is that <div> have different default styling, and you should always remember about this. For example the default value for flexDirection in RN is column, not row like we have with <div>s.
Even though the official RN documentation about View tells us this:
View maps directly to the native view equivalent on whatever platform React Native is running on, whether that is a UIView, <div>, android.view, etc.
We should always keep in mind the specifics of the particular platform we work with.
I would also recommend you to check out this tutorial regarding Flex styling in React Native to get better experience working with it.

BottomTabNavigator coming on top instead of bottom in React Native expo mobile app

I am developing a mobile react native expo app. I am using BottomTabNavigator (NavigationContainer). As the name suggests it should appear at the bottom but it is incorrectly appearing on top.
I already have another image (logo.png) on the top of the screen and the navigationbar (or NavigationContainer) is also coming on top and overlapping above the image. Please help me resolve this issue. See my code below:
In the below code MyTabs is the Navigator created from createBottomTabNavigator(). This is incorrectly appearing on top of the screen.
import React from 'react';
import { Image, StyleSheet, Text, View, SafeAreaView, StatusBar, Platform } from 'react-native';
import logo from './assets/logo.png';
import { NavigationContainer } from '#react-navigation/native';
import MyTabs from './navigator/AppNavigator';
export default function App() {
return (
<SafeAreaView style={{ paddingTop: Platform.OS === 'android' ? StatusBar.currentHeight: 0 }} >
<View>
<View style={styles.container}>
<Image source={logo} style={{ width: 100, height: 100 }} />
<Text style={{color: '#888', fontSize: 18, alignItems: 'center'}}>
To share a photo from your phone with a friend or anyone, just press the button below!
</Text>
</View>
<View >
<NavigationContainer >
<MyTabs /> // This is incorrectly coming on top of screen.
</NavigationContainer>
</View>
</View>
</SafeAreaView>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
// justifyContent: 'center',
},
});
The NavigationContainer should be the outermost component in App. This then wraps the Tab.Navigator component (in your case MyTabs), where you create tabs linked to each of your components. Inside your components, you are able to utilize SafeAreaView to then display the image at the top of the screen. Any type of Navigation scheme has to be made the top most component in the hierarchy in react native, wrapping the rest of your components. I've altered your code below:
import React from 'react';
import { Image,  StyleSheet, Text, View, SafeAreaView, StatusBar, Platform } from 'react-native';
import { NavigationContainer } from '#react-navigation/native';
import { createBottomTabNavigator } from '#react-navigation/bottom-tabs';
export default function App() {
  const Tab = createBottomTabNavigator()
  return (
    <NavigationContainer >
      <Tab.Navigator>  
        <Tab.Screen name="Home" component={myComponent} />
      </Tab.Navigator>
    </NavigationContainer>
  );
}
const myComponent = () => {
  return (
    <SafeAreaView style={{ paddingTop: Platform.OS === 'android' ? StatusBar.currentHeight: 0 }} >
      <View>
        <View style={styles.container}>
          <Image source={require('#expo/snack-static/react-native-logo.png')} style={{ width: 100, height: 100 }} />
          <Text style={{color: '#888', fontSize: 18, alignItems: 'center'}}>To share a photo from your phone with a friend or anyone, just press the button below!</Text>
        </View>
      </View>
    </SafeAreaView>
  )
}
const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
     alignItems: 'center',
    // justifyContent: 'center',
  },
});

React Native Project Structure Issue

I’ve just started using react native and expo and I’ve been restructuring folders over and over again because I run into multiple errors where a js file can’t find an image that’s located under ./asses/navigation/the_image.png. The only time I got it to work was having all the js files within the App.js and have the App.js located under “./” and the pictures in the same location.
This is what the error looks like: error
And this is what my project structure looks like: project structure
What’s the proper way to structure my project and how can I get files such as my profile.js to find images in assets?
Code for messages.js:
import 'react-native-gesture-handler';
import React, { useState } from 'react';
import { Image, StyleSheet, TouchableOpacity, View, TextInput } from 'react-native';
export default function messages({ navigation })
{
const [filtertext, setfilter] = useState('');
return(
<View style={styles.home_container}>
<View style={styles.home_background}>
<TextInput
style= {styles.filter}
placeholder= "Search"
placeholderTextColor= "#96a7af"
onChangeText={filtertext => setfilter(filtertext)}
defaultValue={filtertext}
/>
<Image source= {require ('./assets/navigation/3_Elements_Circled_Navigation_Message_On.png')}/>
<TouchableOpacity onPress={() =>navigation.navigate('Home')} style={styles.home_button}>
<Image source= {require ('./assets/buttons/new_message_icon.png')} style={styles.new_msg_buton}/>
</TouchableOpacity>
<TouchableOpacity onPress={() =>navigation.navigate('Profile')} style={styles.profile_button}>
<Image source= {require ('./assets/buttons/profile_icon.png')}/>
</TouchableOpacity>
</View>
</View>
);
}
const styles = StyleSheet.create(
{
new_msg_buton:
{
position:'absolute',
height: 60,
width: 60,
top: -696,
left: 129,
},
filter:
{
position: 'absolute',
height: 54,
width: 275,
top: -660,
left: 19,
paddingLeft: 20,
borderColor: 'black',
borderRadius: 23,
borderTopWidth: 3,
borderBottomWidth: 3,
borderLeftWidth: 3,
borderRightWidth: 3,
}
})
Using ./ refers to the current directory the file is located in
Using ../dir_onelevel_above refers to the dir_onelevel_above as you've gone one level above in the folder structure.
That being said, your should need ../assets/navigation/icon.png to access the image from messages.js

How to get rid of inline styles added as part of the React Native Web Library?

I am trying to use React Native components in the web by making use of React Native web Library.
But when doing that my DOM elements has lots of inline styles which makes it look cluttered , how can we get rid of the redundant styles as part of the transpilation .
Some thing like this
And can we even get rid of this transplied inline css.
This is how my React Native component used here looks like
import React from "react";
import { View, Text, Platform, StyleSheet } from "react-native";
const instructions = Platform.select({
ios: "Press Cmd+R to reload,\n" + "Cmd+D or shake for dev menu",
android:
"Double tap R on your keyboard to reload,\n" +
"Shake or press menu button for dev menu",
web: "Your browser will automatically refresh as soon as you save the file."
});
const HomeScreen = () => {
return (
<View >
<Text>
Welcome to React Native Web universal app!
</Text>
<Text>
This component is shared between web and react environment. To see how
it works, just edit the HomeScreen.js
</Text>
<Text>{instructions}</Text>
</View>
);
};
/*const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: "center",
alignItems: "center",
backgroundColor: "#F5FCFF"
},
welcome: {
fontSize: 20,
textAlign: "center",
margin: 10
},
instructions: {
textAlign: "center",
color: "#333333",
marginBottom: 5
}
});*/
export default HomeScreen;
After updating the RNW to 0.11.4 the DOM appears to be more cleaner and leaner but dosent eliminate them.
eg

Resources