Skip to content

Conversation

mfenniak
Copy link

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:

    mReactInstanceManager = ReactInstanceManager.builder()
        .setApplication(getApplication())
        ...
        .addTypeFace("DancingScript",
            Typeface.createFromAsset(getAssets(), "DancingScript-Regular.ttf"))
        .build();

And this allows you to use the font, like this:

<Text style={{fontFamily: 'DancingScript'}}>
    Custom font loaded from an application asset TTF file
</Text>

Internally, I've created an interface called a TypefaceProvider that is provided by the ReactContext, and then can be passed through to any text-related view manager to retrieve fonts. The existing caching behavior that was in CustomStyleSpan has been preserved and moved to TypefaceProviderImpl, 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.

@facebook-github-bot
Copy link
Contributor

By analyzing the blame information on this pull request, we identified @mkonicek, @kmagiera and @arbesfeld to be potential reviewers.

@facebook-github-bot facebook-github-bot added GH Review: review-needed CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. labels Oct 30, 2015
@mfenniak mfenniak force-pushed the android-custom-fonts branch from bf429d9 to c21e5c6 Compare November 1, 2015 15:40
@facebook-github-bot
Copy link
Contributor

@mfenniak updated the pull request.

@vovacodes
Copy link

Looks neat! Hope this will be merged. The "custom typeface" problem on Android is very annoying. Thanks for your effort

@facebook-github-bot
Copy link
Contributor

@mfenniak updated the pull request.

@satya164
Copy link
Contributor

satya164 commented Nov 4, 2015

+1

@mfenniak
Copy link
Author

mfenniak commented Nov 7, 2015

Closing this PR; equivalent functionality has been added in bfeaa6a.

@mfenniak mfenniak closed this Nov 7, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Android] Custom Fonts Not Working

5 participants