Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iOS: Add support for fonts #1185

Closed
1 task done
rrebase opened this issue Nov 3, 2022 · 3 comments · Fixed by #1828
Closed
1 task done

iOS: Add support for fonts #1185

rrebase opened this issue Nov 3, 2022 · 3 comments · Fixed by #1828
Labels
enhancement New feature or request platform: iOS This affects iOS

Comments

@rrebase
Copy link

rrebase commented Nov 3, 2022

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
@rrebase rrebase added the enhancement New feature or request label Nov 3, 2022
@tido64 tido64 added the platform: iOS This affects iOS label Nov 3, 2022
@tido64
Copy link
Member

tido64 commented Nov 7, 2022

Hi @rrebase, this sounds like a good thing to add.

In the meantime, if you're on 2.x, have you tried using Expo Config Plugins? We added support for it recently. I have yet to document it, but you can find an example in this PR: #1033

The official documentation can be found here: https://docs.expo.dev/guides/config-plugins/

@rrebase
Copy link
Author

rrebase commented Nov 7, 2022

Haven't tried Expo Config Plugins outside of Expo apps yet, but sounds like a fantastic addition as withInfoPlist for example, is much more flexible and would support setting any other values as well 👍

@tido64 tido64 added this to the Single-app Mode milestone Feb 8, 2024
@tido64
Copy link
Member

tido64 commented Feb 9, 2024

@rrebase Thank you for your initial implementation of this. I have submitted a PR based on it. Sorry it took so long to get to this point.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request platform: iOS This affects iOS
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants