Improve Android fonts linking process #1
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR aims to improve the whole Android fonts linking process.
Problem
Currently, during Android linking, font assets are copied to Android asset's folder and then can be used in the application. The problem is that in RN Android has a different logic to manage fonts related to iOS, requiring the developer to rely on the font file's name to be able to use the font properly in the application.
To give an example, let's say I added Lato font to my project and linked the files in both Android and iOS platforms. These are the Lato font files:
For iOS, I can simply use a combination of
fontFamily,fontWeightandfontStylestyles to select the desired font:For Android, this approach won't work and you have to rely on the font's name in order to select the desired font.
Developers end up having to use different approaches in order to custom fonts work properly for both platforms.
Solution
This PR solves the stated problem by changing the Android linking process to do the steps described in this guide automatically. In Android, fonts are now going to be managed by using XML Fonts to handle all font's variants.
During font assets copying process, here is the following logic:
res/font/folder and normalize their names.ReactFontManager's import toMainApplication.javafile if it isn't declared yet.onCreate()to load the custom font on Android.During font assets cleaning process, here is the following logic:
res/fontfolder that needs to be deleted.MainApplication.java.ReactFontManagerinMainApplication.java, remove the import as well.