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

fix: dynamic font scaling support for inputs and buttons #284

Closed

Conversation

hurshore
Copy link

Pull Request Template

Description:

This PR introduces a new useFontScale hook to address text scaling issues in buttons and inputs on iOS and Android. Currently, when users increase their device's font size settings, text can get cut off in UI elements since they don't scale accordingly.

The hook provides utilities to dynamically adjust component heights based on the system font scale, ensuring text remains visible and properly contained within UI elements. It uses React Native's useWindowDimensions to reactively update when system font settings change.

Fixes #219

Tested Platforms:

  • Web
  • iOS
  • Android

Affected Apps/Packages:

  • ui

Screenshots:

  1. Input/button with default font scale
Default font size input screenshot Large font size input screenshot
  1. Input/button with increased font scale
Default font size screenshot Large font size screenshot

Notes:

  • The hook provides three utilities:
    • fontScale: current system font scale factor
    • getScaledSize: scales any size value according to system font scale
    • getScaledHeight: calculates appropriate container height based on font size
  • Components using this hook will automatically adjust their heights when system font size changes
  • Implementation preserves existing web functionality while only applying scaling on native platforms
  • The solution maintains consistent appearance across different device font size settings without requiring app restart

Example usage in components:

const { getScaledHeight } = useFontScale();
const baseHeight = 48;
const baseFontSize = 16;

// Component height will now scale with system font size
const dynamicHeight = getScaledHeight(baseHeight, baseFontSize);

Copy link

vercel bot commented Nov 23, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
rnr-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 23, 2024 10:19pm
rnr-showcase ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 23, 2024 10:19pm

@mrzachnugent
Copy link
Owner

Hi @hurshore , thank you for taking the time to create this PR. Unfortunately, I cannot merge it for the following reasons:

  1. Using the style attribute means that it will override the className. This might be what you want for a given case but it goes against the feature of have class names override default class names with the cn function
  2. The reason NativeWind doesn't behave on mobile as it does on web for the "rem" value is due to performance (see https://www.nativewind.dev/tailwind/typography/font-size#rem-scaling)

"To improve performance NativeWind will inline rem values on all platforms, except for Web"

  1. We are still unsure how we want to approach this, perhaps through a CLI to initialize projects. If we do, we would have to do it through the suggested way https://www.nativewind.dev/customization/theme#fontscale

I see that you have put thought and effort into this PR so I would suggest that you add your method of scaling in the Discussions section.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[ FEATURE ] font scaling adaptive inputs
2 participants