Skip to content

Supporting custom fonts the Android Way #19736

@dulmandakh

Description

@dulmandakh

For Discussion

Android SDK 26 introduced easy way to support custom fonts, where users define font family using XML and use it with ResourcesCompat.getFont(context, R.font.myfont). Read https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml

Current React Native support for custom fonts on Android implemented in ReactFontManager and CustomStyleSpan, and it's hard to use font weights other than bold and regular (normal), like medium, light etc. To use custom font weights on Android, I have to specify font file name without extension in fontFamily of style, which breaks iOS apps. Therefore, I check the platform in JS to support both Android and iOS, and set fontFamily and fontWeight accordingly.

Adding support for Android Way seemed trivial with fallback to old way.

  1. check if font resource is exists
  2. if exists, get resource id and get typeface using ResourcesCompat.getFont(context, )
  3. else fallback to old way (ReactFontManager)

But, font styling of Text in Android implemented in ReactBaseTextShadowNode, which have no access to context. So, I think support custom fonts the Android Way will require refactoring of Text and decide if RN will support the OLD WAY.

Please consider this for FabricUIManager

CC: @hramos @mdvacca

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions