I',m making some changes to the text in my react native application: I need to specify Roboto light for paragraphs and Roboto Bold for titles.
I need to have the same look of the text in both iOS and Android apps: so I need to make it work for both
I tried this code line of code
text : {
fontFamily: 'sans-serif-light'
},
but I get this error:
I tried this type from the official documentation and it's working fine
title : {
fontFamily: 'Cochin'
},
--> So I think the problem is in the Roboto font family itself. Any help?
To add custom fonts to your app store all your ttf files in a directory.
Add the following code to your package.json file.
"rnpm": {
"assets": [
"./fonts" // yours fonts directory
]
}
Then run react-native link
To use the font use the same name on the ttf file in fontFamily.
sans-serif-light and Roboto are Android-only fonts. You need different fonts for iOS. This repository has a list of fonts available for iOS and Android - https://github.com/react-native-training/react-native-fonts
You can use Platform.select() to target different fonts for each OS:
title: {
...Platform.select({
ios: { fontFamily: 'Arial', },
android: { fontFamily: 'Roboto' }
})
}
I recently had the same issue . It turns out that rnpm command is deprecated and you can add custom assets using react native cli configuration.
https://github.com/react-native-community/cli/blob/master/docs/configuration.md
To add fonts in the project:
Download the font and place it in the assets/fonts folder
Create a file in the project root directory as react-native.config.js
Add the following code
module.exports={
assets:[
"./assets/fonts"
]
}
Run react-native link
Run the project : npx react-native run-ios
Note: if build failed for IOS, open xocde workspace settings and change the build system to Legacy Build System.
Setup Roboto for both Android/iOS:
Usage
As Roboto is the default font family for Android. We can add Roboto to iOS and just use RRikesh solution omiting fontFamily for Android:
import {
Platform,
StyleSheet,
} from 'react-native'
const stylesByPlatform = Platform.select({
ios: { fontFamily: 'Roboto' },
android: { },
})
export default StyleSheet.create({
text: {
...stylesByPlatform,
color: '#000000',
},
})
Setup
For iOS we need to add Roboto fontFamily:
Download Roboto font from Google Fonts
Add it to your assets folder ./assets/fonts/Roboto
Add assets folder to your package.json:
{
...
"rnpm": {
"assets": [
"./assets/fonts"
]
}
}
Run: react-native link (it links ttf files on iOS and copy them on Android)
Remove Roboto files added in android/app/src/main/assets/fonts
Rebuild your app and 🎉.
I really don't know why this type of content is not in official docs. :(
If it can help, I had a similar issue. Using "react-native link" did indeed referenced the fonts in "Build Phases > Copy Bundle Resource", but it didn't add anything to the Info.plist file.
Adding the fonts in Info.plist solved the issue.
<key>UIAppFonts</key>
<array>
<string>Roboto-Black.ttf</string>
</array>
for react-native +v0.69, npx react-native link will produce an error. If you are using this version of react-native the solution is;
npm i react-native-asset or yarn add react-native-asset
create a react-native.config.js file in the root folder of your project.
Add the following to the file
module.exports = { project: { ios: {}, android: {}, }, assets: ['./assets/fonts/'], };
create a assets/fonts folder at the root of your project.
then in the terminal, run npx react-native-asset.
That should solve your problem.
NB: As of rn-v0.69, react-native link has been discontinued and replaced with autolinking, but this feature doesn't work with adding custom fonts to your project so react-native-asset provides the same fixes as react-native link.
Related
From like hour ago I keep getting network error on material design icons cdn as error name not resolved.
http://cdn.materialdesignicons.com/5.4.55/css/materialdesignicons.min.css
I am wondering why is nuxt app using cdn instead of downloaded files?
When I try to run npm install nuxt-material-design-icons or npm install material-design-icons it just freezes..
I figured it out,
Step 1: Add Google fonts icon's CDN.
export default {
head () {
return {
link: [
// Add this
{ rel: 'stylesheet', href: 'https://fonts.googleapis.com/icon?family=Material+Icons' }
],
}
}
}
Step 2: Use it (you are done!)
<template>
// Add this
<span class="material-icons">face</span>
</template>
Here is the complete list of Material Icons.
For Nuxt.js, you will need to install this package:
yarn add nuxt-material-design-icons-iconfont
// Or if you are using npm:
npm install nuxt-material-design-icons-iconfont
And declare that package within nuxt.config.js file:
modules: [
'nuxt-material-design-icons-iconfont',
],
Add "nuxt-material-design-icons" in "modules" in nuxt.config.js
I'm using scss to style my next.js app however I noticed that all styles are missing when I preview the first paint via the network tab in google chrome.
As you can see the page is fairly simple and does not fetch any initial props. I'm suspecting that the server is for some reason not building the whole page (including styles) on the backend.
My next.config.js setup is:
const withStyles = require("#webdeb/next-styles");
module.exports = withStyles({
sass: true, // use .scss files
modules: true, // style.(m|module).css & style.(m|module).scss for module files
});
With newer nextjs versions, I don't think you need to use withStyles and similar plugins anymore.
global styles only
You can just:
install sass
install whatever css framework you're using and follow their instructions for how to include their css / scss files in your project
potentially get rid of your next.config.js file entirely if it's a simple project and all you had in the config file was css configuration stuff.
import your scss files directly into your js files (eg import '../styles/app.scss')
scoped scss styles
seems you can keep the next.config.js file, based on this workaround I found here through a google search:
const withStyles = require('#webdeb/next-styles')
module.exports = withStyles({
sass: true, // use .scss files
modules: true, // style.(m|module).css & style.(m|module).scss for module files
})
I was installed Nuxt via command npx create-nuxt-app with Vuetify plugin. Then I ran server via npm run dev or npm run build && npm start and in page source I see CSS styles of Vuetify in <head> tag. After some googleing I found advice add extractCSS: true, in build section of nuxt.config.js. After that when I run npm run build && npm start css files are generating and linking to pages, but I still see some CSS styling (~500 lines) in <style data-n-head="true" id="vuetify-theme-stylesheet" type="text/css">. How I can hide them to CSS file instead of displaying in <head> tag.
And how I can extract CSS when start npm run dev (pretty annoying to scroll this CSS when debug HTML layout)?
Not sure if this is exactly the same issue but I found had many lines of Vuetify colours CSS in the head of each pre-rendered html file. I eventually found https://vuetifyjs.com/en/features/theme/#variations.
When Vuetify generates your application’s theme, it creates 9 variants for each color. For majority of users, these variants are rarely used. This is an opt in feature that will be false by default in the next major version.
// src/plugins/vuetify.js
import Vue from 'vue'
import Vuetify from 'vuetify/lib'
export default new Vuetify({
theme: {
options: { variations: false },
},
})
Adding options: { variations: false } as above (along with extractCSS: true in Nuxt.config.js) got rid of all superfluous CSS in the pre-rendered html.
Am having a small problem with opening/importing a react-native into Android studio.
If I open the project using the open a project dialog, it tells me that the project is not gradle enabled and it is such a pain to make and test code changes. Couldn't find out how to enable the project as a gradle project after the fact even after going through the material on the help site.
On the other hand, if I import using the import a gradle project dialog and select the build.gradle file, the project is imported, but I only see the files inside the android directory instead of the main project directory. But this method allows me to push changes easily to the emulator.
How can I fix my problem?
Thanks,
Just import android directory from Android Studio,
make changes to your app/build.gradle
add these codes before apply from: "../../node_modules/react-native/react.gradle"
project.ext.react = [
bundleAssetName: "index.android.bundle",
entryFile: "index.android.js",
bundleInDebug: false,
bundleInRelease: true,
root: "../../",
jsBundleDirDebug: "$buildDir/intermediates/assets/debug",
jsBundleDirRelease: "$buildDir/intermediates/assets/release",
resourcesDirDebug: "$buildDir/intermediates/res/merged/debug",
resourcesDirRelease: "$buildDir/intermediates/res/merged/release",
inputExcludes: ["android/", "ios/"],
nodeExecutableAndArgs: ["node"],
extraPackagerArgs: []
]
Now create task for "react-native start" to your gradle
task startReactNative(type: Exec) {
workingDir "../../"
commandLine 'cmd', '/c', 'react-native', 'start'
println "Working Directory for React is: $workingDir"
standardOutput = new ByteArrayOutputStream()
ext.output = {
println "React-Native Output: " + standardOutput.toString()
return standardOutput.toString()
}
}
You can run your app as usual, after app installed to your device, run startReactNative task in order to activate Hot Reload
I am having an issue with brunch#1.7.6 not compiling bower_component css files. Similar to Separating app and vendor css in Brunch. Only the css/app.css is getting generated for me. :/
{
stylesheets: {
joinTo: {
'css/app.css': /^app/,
'css/vendor.css': /^bower_components/
}
}
Please let me know if I am doing something wrong. All seemed fine when I was using brunch#1.6.7. Did the config change with the introduction to bower being built in?
With regards to x-editable, I ran into the failure to include the bower_components css, but found that I simply had to add the specifics in the overrides section of bower.json (since it has multiple library options the main is not included in the .bower.json).
I used the following overrides successfully:
"overrides": {
"x-editable": {
"main": [
"dist/bootstrap3-editable/js/bootstrap-editable.js",
"dist/bootstrap3-editable/css/bootstrap-editable.css"
]
}
}