-
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
Open
ArekChr
wants to merge
24
commits into
facebook:main
Choose a base branch
from
ArekChr:improve-lineheight-calc-ios
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
f998a9a
improve text line height calculation on ios
ArekChr 8168cf0
add comments
ArekChr 164bf0c
add safeguards to prevent out-of-bounds crash when accessing text att…
ArekChr f79a614
Merge branch 'main' into improve-lineheight-calc-ios
ArekChr b044af9
codegen feature flags
ArekChr 4844731
Fix namespace for ReactNativeFeatureFlags in RCTTextView
ArekChr 76faa56
refactor: separate feature flat per platform
ArekChr 9e5ed70
fix: add React-featureflags pod for dynamic frameworks configuration
ArekChr 09118be
Merge branch 'main' into improve-lineheight-calc-ios
ArekChr 193d804
Merge branch 'main' into improve-lineheight-calc-ios
ArekChr a41f8c8
Merge branch 'main' into improve-lineheight-calc-ios
ArekChr 2ec676c
Merge branch 'main' into improve-lineheight-calc-ios
ArekChr 848e4b2
Merge branch 'main' into improve-lineheight-calc-ios
ArekChr 045ced9
Merge branch 'main' into improve-lineheight-calc-ios
ArekChr c46a96f
update feature flags
ArekChr d4e44de
fix: feature flag import in rn-tester dynamic frameworks
ArekChr b950279
Merge branch 'main' into improve-lineheight-calc-ios
ArekChr 3610212
fix: update feature flags
ArekChr ffc5503
Merge remote-tracking branch 'upstream/main' into improve-lineheight-…
ArekChr 7fd5214
Merge remote-tracking branch 'upstream/main' into improve-lineheight-…
ArekChr 4e35916
fix: add missing expectedReleaseValue to feature flags config
ArekChr 81e560b
refactor: remove unused flag on android
ArekChr a52fb88
Merge remote-tracking branch 'upstream/main' into improve-lineheight-…
ArekChr 3a13ddf
Merge branch 'main' into improve-lineheight-calc-ios
ArekChr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
|
||
#import <React/RCTUtils.h> | ||
#import <React/UIView+React.h> | ||
#import <react/featureflags/ReactNativeFeatureFlags.h> | ||
|
||
#import <React/RCTTextShadowView.h> | ||
|
||
|
@@ -98,6 +99,36 @@ - (void)setTextStorage:(NSTextStorage *)textStorage | |
[self setNeedsDisplay]; | ||
} | ||
|
||
- (CGPoint)calculateDrawingPointWithTextStorage:(NSTextStorage *)textStorage | ||
contentFrame:(CGRect)contentFrame { | ||
UIFont *font = [textStorage attribute:NSFontAttributeName atIndex:0 effectiveRange:NULL]; | ||
if (!font) { | ||
font = [UIFont systemFontOfSize:14]; | ||
} | ||
|
||
NSParagraphStyle *paragraphStyle = [textStorage attribute:NSParagraphStyleAttributeName atIndex:0 effectiveRange:NULL]; | ||
|
||
CGFloat lineHeight = font.lineHeight; | ||
if (paragraphStyle && paragraphStyle.minimumLineHeight > 0) { | ||
lineHeight = paragraphStyle.minimumLineHeight; | ||
} | ||
|
||
CGFloat ascent = font.ascender; | ||
CGFloat descent = fabs(font.descender); | ||
CGFloat textHeight = ascent + descent; | ||
|
||
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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. Sure, added comments |
||
|
||
return CGPointMake(contentFrame.origin.x, contentFrame.origin.y + verticalOffset); | ||
} | ||
|
||
- (void)drawRect:(CGRect)rect | ||
{ | ||
[super drawRect:rect]; | ||
|
@@ -118,8 +149,15 @@ - (void)drawRect:(CGRect)rect | |
#endif | ||
|
||
NSRange glyphRange = [layoutManager glyphRangeForTextContainer:textContainer]; | ||
[layoutManager drawBackgroundForGlyphRange:glyphRange atPoint:_contentFrame.origin]; | ||
[layoutManager drawGlyphsForGlyphRange:glyphRange atPoint:_contentFrame.origin]; | ||
|
||
if (ReactNativeFeatureFlags::enableLineHeightCentering()) { | ||
CGPoint drawingPoint = [self calculateDrawingPointWithTextStorage:_textStorage contentFrame:_contentFrame]; | ||
[layoutManager drawBackgroundForGlyphRange:glyphRange atPoint:drawingPoint]; | ||
[layoutManager drawGlyphsForGlyphRange:glyphRange atPoint:drawingPoint]; | ||
} else { | ||
[layoutManager drawBackgroundForGlyphRange:glyphRange atPoint:_contentFrame.origin]; | ||
[layoutManager drawGlyphsForGlyphRange:glyphRange atPoint:_contentFrame.origin]; | ||
} | ||
|
||
__block UIBezierPath *highlightPath = nil; | ||
NSRange characterRange = [layoutManager characterRangeForGlyphRange:glyphRange actualGlyphRange:NULL]; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.