Skip to content

iOS: Add support for fontsΒ #1185

@rrebase

Description

@rrebase

Proposal

In iOS, adding a custom font requires updating UIAppFonts in Info.plist https://developer.apple.com/documentation/uikit/text_display_and_fonts/adding_a_custom_font_to_your_app

Looks like it's not supported right now so I propose an additional fonts property under platform in the Manifest (app.json)

{
  "ios": {
    "fonts": [
      "EuclidCircularB-Regular.ttf"
      "EuclidCircularB-Bold.ttf",
    ]
  }
}

Implementation Details

The reading of manifest on iOS could be similar to app icons

generate_assets_catalog!(project_root, target_platform, destination)
and then parse, update the plist using CFPropertyList (subdep of current gems). Here's some code (thanks CodePilot), tested in example and it works.

require("cfpropertylist")

# ...

def set_fonts_in_plist!(project_root, target_platform, destination)
  fonts = platform_config('fonts', project_root, target_platform)
  return if fonts.nil? || fonts.empty?

  info_plist = File.join(destination, 'ReactTestApp', 'Info.plist')

  plist = CFPropertyList::List.new(file: info_plist)
  data = CFPropertyList.native_types(plist.value)

  data['UIAppFonts'] = []
  fonts.each do |font|
    data['UIAppFonts'] << File.basename(font)
  end

  plist.value = CFPropertyList.guess(data)
  File.write(info_plist, plist.to_str(CFPropertyList::List::FORMAT_XML))
end

# ...
generate_assets_catalog!(project_root, target_platform, destination) 
set_fonts_in_plist!(project_root, target_platform, destination)

Also worth adding the font files automatically to resources, otherwise they need to be duplicated in resources.ios[].

Code of Conduct

  • I agree to follow this project's Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions