nativewind icon indicating copy to clipboard operation
nativewind copied to clipboard

How to use custom fonts

Open ImamJanjua opened this issue 1 year ago • 5 comments

Like how can I use custom fonts with nativewind.

ImamJanjua avatar Jul 04 '24 17:07 ImamJanjua

You can extend your tailwind.config.js, here is mine

  theme: {
    colors,
    extend: {
      fontFamily: {
        'poppins-regular': ['Poppins_400Regular'],
        'poppins-medium': ['Poppins_500Medium'],
        'poppins-semibold': ['Poppins_600SemiBold'],
      },
    },
  },

don't forget to load your font before starting the app for example, this is what I'm doing in my App.tsx

import {
useFonts,
Poppins_400Regular,
Poppins_500Medium,
Poppins_600SemiBold,
} from '@expo-google-fonts/poppins'

const [fontsLoaded] = useFonts({
  Poppins_400Regular,
  Poppins_500Medium,
  Poppins_600SemiBold,
})

and use the custom font like this

import {Text} from 'react-native'
<Text className="font-poppins-semibold" />

martinezguillaume avatar Jul 12 '24 07:07 martinezguillaume

@martinezguillaume I followed your suggestion, and this is what I did, inside app.json I loaded the fonts using

[
  "expo-font",
  {
    "fonts": [
      "./node_modules/@expo-google-fonts/quicksand/Quicksand_300Light.ttf",
      "./node_modules/@expo-google-fonts/quicksand/Quicksand_400Regular.ttf",
      "./node_modules/@expo-google-fonts/quicksand/Quicksand_500Medium.ttf",
      "./node_modules/@expo-google-fonts/quicksand/Quicksand_600SemiBold.ttf",
      "./node_modules/@expo-google-fonts/quicksand/Quicksand_700Bold.ttf"
    ]
  }
]

The tailwind config

fontFamily: {
  Quicksand_300Light: ["Quicksand_300Light"],
  Quicksand_400Regular: ["Quicksand_400Regular"],
  Quicksand_500Medium: ["Quicksand_500Medium"],
  Quicksand_600SemiBold: ["Quicksand_600SemiBold"],
  Quicksand_700Bold: ["Quicksand_700Bold"],
},

But I can't call the font using font-Quicksand_*** , I only able to call the font using fontFamily

<Text
  className="text-xl"
  style={{ fontFamily: "Quicksand_300Light" }}
>
  wakanda
</Text>
<Text
  className="text-xl"
  style={{ fontFamily: "Quicksand_400Regular" }}
>
  wakanda
</Text>
<Text
  className="text-xl"
  style={{ fontFamily: "Quicksand_500Medium" }}
>
  wakanda
</Text>
<Text
  className="text-xl"
  style={{ fontFamily: "Quicksand_600SemiBold" }}
>
  wakanda
</Text>
<Text className="text-xl" style={{ fontFamily: "Quicksand_700Bold" }}>
  wakanda
</Text>

Screenshot 2024-08-06 at 04 04 09

Do you know which part that I'm missing?

arryanggaputra avatar Aug 05 '24 21:08 arryanggaputra

Hello @arryanggaputra ! Try

// tailwind.config.js
const {platformSelect} = require('nativewind/theme')
...
      fontFamily: {
        'quicksand-regular': platformSelect({
          android: 'Quicksand_400Regular',
          ios: 'Quicksand-Regular',
        }),
        'quicksand-medium': platformSelect({
          android: 'Quicksand_500Medium',
          ios: 'Quicksand-Medium',
        }),
        'quicksand-semibold': platformSelect({
          android: 'Quicksand_600SemiBold',
          ios: 'Quicksand-SemiBold',
        }),
        ...
      },

and after

      <Text className="font-quicksand-semibold">

Font family names is not the same between android & ios as stated here

martinezguillaume avatar Aug 05 '24 21:08 martinezguillaume

Hi @martinezguillaume, it works now :). thank you. Can you tell me why we can't use this?

'Quicksand_600SemiBold': platformSelect({
      android: 'Quicksand_600SemiBold',
      ios: 'Quicksand-SemiBold',
}),

arryanggaputra avatar Aug 06 '24 23:08 arryanggaputra

I think every tailwind shorthands need to be kebab-case

martinezguillaume avatar Aug 07 '24 05:08 martinezguillaume

We are closing all pre-v4.1 issues now that v4.1 is released. If the problem is still happening with the latest v4.1, please open a new issue and reference this one in the description.

danstepanov avatar Sep 02 '24 23:09 danstepanov