Open flixyudh opened 10 months ago
Use fontFamily name instead of fontWeight
ex : use FamilyName-Medium instead of familyName + 500
@Linuhusainnk I'm not asking how to use it, I'm asking why fontWeight
is not apply when using custom font? the font itself support all fontWeight
from 100-900
Please check below things are done properly
and to answer your question please check https://github.com/facebook/react-native/issues/26193
As far as I have understood, this issue has unfortunately existed for years for Android.
Here's a great workaround of how you can make the fontWeight
property work properly by utilising Android's XML font feature. Note that this workaround doesn't work with EXPO without ejecting.
Stack overflow answer: https://stackoverflow.com/a/70247374 Step-by-step guide: https://github.com/jsamr/react-native-font-demo
Use fontFamily name instead of fontWeight
ex : use FamilyName-Medium instead of familyName + 500
I use this workaround
function mapFontweightToFontFamily(weight: FontWeight, fontPrefix = "Mulish") {
switch (weight) {
case "extralight":
return `${fontPrefix}ExtraLight`;
case "light":
return `${fontPrefix}Light`;
case "regular":
return `${fontPrefix}`;
case "semibold":
return `${fontPrefix}SemiBold`;
case "bold":
return `${fontPrefix}Bold`;
case "extrabold":
return `${fontPrefix}ExtraBold`;
case "black":
return `${fontPrefix}Black`;
default:
return `${fontPrefix}`;
}
}
Do you still experience this issue? If yes, I will publish the fix in the react-native-improved package https://github.com/fabriziobertoglio1987/react-native-improved. Thanks a lot
I can confirm that I am also running into this issue and the problem with the proposed workaround is that you need to add these font files to your app which wouldn't be needed when using fontWeight.
@fabriziobertoglio1987 yes, the issue still persist.
i don't really understand why we need react-native-improved
to fix this issue, is it not possible to fix into react-native
core, so you need to create a patch to support fontWeight
with custom font? or the patch itself is a temporary solution while we waiting RN Team fixed the issue?
i think it should be fixed in react-native
source, since Text
itself is a part of react-native
core component.
@fabOnReact @cortinico can we override the ReactTypefaceUtils.java
to something like this ?
ReactTypefaceUtils.java
Based on 0.73.1:
I've tried to compile then run it, and the result quite expected except the fontWeight 700, 800, 900
seems not affected
Current Result with <Text/>
Component :
Current Result with <TextInput/>
Component :
You can over ride everything. Even private apis. You can check my project sourcecode to understand what i mean
Just define new class that inherits from that java api
My project has many examples of this
Private api needs to be reimplemented in child class.. but few lines of code
@cortinico Im not a java expert but this doesnt feel right com/facebook/react/views/text/ReactTypefaceUtils.java
:
The RN upgrade has made the android version ugly
Im not a java expert but this doesnt feel right
com/facebook/react/views/text/ReactTypefaceUtils.java
:
Can you clarify why it doens't feel right @krisidmisso
@cortinico
"bold"
case in parseFontStyle
? Again, I dont understand java but this issue is boring and it challenges us in every project. I have bookmarked this solution https://stackoverflow.com/questions/38815234/how-to-add-fonts-for-different-font-weights-for-react-native-android-project but I dont think this should be the best way to achieve font styles
I dont remember having this issue with old RN versions
I dont remember having this issue with old RN versions
Hello Everyone! I'm running into the same problem and trying to wrap my head around the issue. A couple of things don't add up for me.
Font weight on Android for custom fonts only applies regular weight for 100 - 600 and bold for 700 - 900. For default font family however it works fine for all the ranges.
If it's Android's limitation and the only way to make it work is to make an XML font definition as described in this comment: https://github.com/facebook/react-native/issues/42116#issuecomment-1876854067 then does it mean the Android system font has separate fonts defined for Thin, ExtraLight, Light, Regular, Medium, SemiBold, Bold ,ExtraBold and Black?
In that case it doesn't make sense to me because I couldn't find the place where RN converts 100- 900 values to Android font names but default RN text still works.
Please let me know if I'm missing something and how I can help 🙏
Description
I just tried to use custom font in my project template, and i realize when i want to use
fontWeight
as a reference of my custom font family it's not applicable, is it expected or am i doing something wrong while linking my custom font?I'm expecting
fontWeight
should applicable with font as google fonts declaredSteps to reproduce
pt
prefix)App.[js/tsx]
React Native Version
0.73.1
Affected Platforms
Runtime - Android
Output of
npx react-native info
Reproducer
https://snack.expo.dev/w667bnl74
Screenshots and Videos