-
Notifications
You must be signed in to change notification settings - Fork 24.4k
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: Improve text line height calculation to properly align text on iOS #46884
base: main
Are you sure you want to change the base?
Conversation
Hi @ArekChr! Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at [email protected]. Thanks! |
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
contentFrame:(CGRect)contentFrame { | ||
UIFont *font = [textStorage attribute:NSFontAttributeName atIndex:0 effectiveRange:NULL]; | ||
if (!font) { | ||
font = [UIFont systemFontOfSize:14]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[Q] Maybe we can use systemFontSize
here?
https://developer.apple.com/documentation/uikit/uifont/1623395-systemfontsize?language=objc
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I couldn’t find systemFontSize used anywhere else in the repo, and the default font size of 14 has been hardcoded. Also, since systemFontSize is not available on tvOS, I’m not sure if it would be a suitable replacement. Let me know your thoughts on whether we should stick with the hardcoded value for consistency or if there’s a preferred approach.
CGFloat verticalOffset = 0; | ||
if (textHeight > lineHeight) { | ||
CGFloat difference = textHeight - lineHeight; | ||
verticalOffset = difference / 2.0; | ||
} else if (textHeight < lineHeight) { | ||
CGFloat difference = lineHeight - textHeight; | ||
verticalOffset = -(difference / 2.0); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think comment explaining the calculations might be useful
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, added comments
nice! |
246c550
to
f998a9a
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changes looks good. We discussed this internally and we can proceed importing it.
To set the expectation, this might take some time to merge, given that this might affect internal tests and product code and we would need some time to fix them. But thankfully, we have a feature flag to control the rollout! 😄
@cipolleschi has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator. |
@ArekChr CI is currently red. The error is
Could you please verify why this is happening and fix it?
and then build from Xcode. If you need to run the old architecture, you can disable it by adding: - (BOOL) newArchEnabled
{
return NO;
} in the AppDelegate.mm file |
Hi @cipolleschi, the issue has been fixed, and the build runs successfully locally. Thanks! |
We were discussing internally that it would be better to have two separate feature flags, one for Android and one for iOS. Could I ask you to:
The idea is to be able to control the two platforms separatedly. Thank you so much for your help and patience |
@cipolleschi I have an issue where the ReactNativeFeatureFlags.h header is not found when using dynamic frameworks. I’ve tried a few things but haven’t resolved the issue. Do you have any suggestions on how to fix or work around this? |
fa44b24
to
9e5ed70
Compare
Hi, @cipolleschi. The CI failed due to some issues unrelated to my changes. I’ve likely fixed the problem with dynamic frameworks, but two workflows seem missing. Could you please trigger those workflows again so we can proceed? |
could you rebase on top of main, please? 🙏 |
@cipolleschi has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator. |
@cipolleschi has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator. |
Are there any updates on this? |
@ArekChr could you rebase on main? |
@cipolleschi sure, done! |
JS tests and the linter are failing. :( Could you have a look at those failures, please? |
@cipolleschi Fixed |
@cipolleschi has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator. |
@cipolleschi has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator. |
@cipolleschi has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator. |
1 similar comment
@cipolleschi has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator. |
Any progress on this? 👀 |
Summary:
This pull request addresses an issue in React Native on iOS: Text gets truncated when the line height is too small, even if the line-height matches or exceeds the font size.
The existing logic did not correctly handle font metrics (ascent, descent, top, bottom) when the total height exceeded the line height. The implementation now ensures a more balanced vertical space distribution, reducing text clipping and misalignment.
The fix mirrors a similar solution made on Android, ensuring consistency across both platforms.
For more context, see the related issue: React Native issue #29507.
Changelog:
[IOS] [FIXED] - Fixed an issue where text clipping and misalignment occurred when the line-height was smaller than the total ascent and descent of the font.
Test Plan:
The result is an improvement over the current implementation, reducing the occurrence of text truncation. While minimal truncation still exists at single-line heights, particularly with special characters, the implementation better preserves the vertical alignment of the text compared to previous behavior. However, like the Android implementation, this does not fully resolve the issue of React Native truncating content that is out of bounds of the span.