Android: Allow custom Typeface to be added ReactInstanceManager #3800
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.
Attached PR adds support for custom, non-system fonts in Android applications. It also adds a demonstration of this capability in the Android UIExplorer application.
Android doesn't have an equivalent to iOS's approach, where you can add fonts to your application's resources and they are automatically available by their font-family name. In fact, Android doesn't have any "global" mechanism to provide fonts in an application that are available by their name. So, the approach taken here is a reasonable platform-specific solution given the disparity in this area between iOS and Android.
To add a custom font, you register a Typeface object on your ReactInstanceManager.Builder, like this:
And this allows you to use the font, like this:
Internally, I've created an interface called a
TypefaceProvider
that is provided by theReactContext
, and then can be passed through to any text-related view manager to retrieve fonts. The existing caching behavior that was inCustomStyleSpan
has been preserved and moved toTypefaceProviderImpl
, augmented with the ability to reference the typefaces that have been added to the react instance manager. I don't love the fact that the TypefaceProvider needs to be passed around so much, but it seems like something would need to be passed into the text view managers to accomplish this without using global state, so I can't think of a better approach.Would resolve #2919.