-
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
Inverted descent/ascent Android prioritisation to match iOS lineHeight behaviour #16448
Closed
bartolkaruza
wants to merge
6
commits into
facebook:master
from
bartolkaruza:feature/fix-inconsistent-lineheight-android
Closed
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
80f15bf
Inverted descent/ascent prioritization to match iOS
6a2dbb1
fix ascent in case lineHeight is bigger than fontMetrics
6ac5133
revert fix to match style of initial implementation
5dc3294
add license header
b063523
Revert the committed change in behaviour from previous commit
c7e373e
fixed issue for large lineHeight and small lineHeight cases
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
74 changes: 74 additions & 0 deletions
74
ReactAndroid/src/test/java/com/facebook/react/views/text/CustomLineHeightSpanTest.java
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 |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package com.facebook.react.views.text; | ||
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. This is missing the license header, mind adding this so we can land this change? |
||
|
||
import android.graphics.Paint; | ||
|
||
import static org.fest.assertions.api.Assertions.assertThat; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.powermock.core.classloader.annotations.PowerMockIgnore; | ||
import org.robolectric.RobolectricTestRunner; | ||
|
||
@RunWith(RobolectricTestRunner.class) | ||
@PowerMockIgnore({"org.mockito.*", "org.robolectric.*", "android.*"}) | ||
public class CustomLineHeightSpanTest { | ||
|
||
@Test | ||
public void shouldIncreaseTop() { | ||
CustomLineHeightSpan customLineHeightSpan = new CustomLineHeightSpan(21); | ||
Paint.FontMetricsInt fm = new Paint.FontMetricsInt(); | ||
fm.top = -10; | ||
fm.ascent = -5; | ||
fm.descent = 5; | ||
fm.bottom = 10; | ||
customLineHeightSpan.chooseHeight("Hi", 0, 2, 0, 0, fm); | ||
assertThat(fm.top).isEqualTo(-11); | ||
assertThat(fm.ascent).isEqualTo(-5); | ||
assertThat(fm.descent).isEqualTo(5); | ||
assertThat(fm.bottom).isEqualTo(10); | ||
} | ||
|
||
@Test | ||
public void shouldReduceTopFirst() { | ||
CustomLineHeightSpan customLineHeightSpan = new CustomLineHeightSpan(19); | ||
Paint.FontMetricsInt fm = new Paint.FontMetricsInt(); | ||
fm.top = -10; | ||
fm.ascent = -5; | ||
fm.descent = 5; | ||
fm.bottom = 10; | ||
customLineHeightSpan.chooseHeight("Hi", 0, 2, 0, 0, fm); | ||
assertThat(fm.top).isEqualTo(-9); | ||
assertThat(fm.ascent).isEqualTo(-5); | ||
assertThat(fm.descent).isEqualTo(5); | ||
assertThat(fm.bottom).isEqualTo(10); | ||
} | ||
|
||
@Test | ||
public void shouldReduceBottomSecond() { | ||
CustomLineHeightSpan customLineHeightSpan = new CustomLineHeightSpan(14); | ||
Paint.FontMetricsInt fm = new Paint.FontMetricsInt(); | ||
fm.top = -10; | ||
fm.ascent = -5; | ||
fm.descent = 5; | ||
fm.bottom = 10; | ||
customLineHeightSpan.chooseHeight("Hi", 0, 2, 0, 0, fm); | ||
assertThat(fm.top).isEqualTo(-5); | ||
assertThat(fm.ascent).isEqualTo(-5); | ||
assertThat(fm.descent).isEqualTo(5); | ||
assertThat(fm.bottom).isEqualTo(9); | ||
} | ||
|
||
@Test | ||
public void shouldReduceAscentThird() { | ||
CustomLineHeightSpan customLineHeightSpan = new CustomLineHeightSpan(9); | ||
Paint.FontMetricsInt fm = new Paint.FontMetricsInt(); | ||
fm.top = -10; | ||
fm.ascent = -5; | ||
fm.descent = 5; | ||
fm.bottom = 10; | ||
customLineHeightSpan.chooseHeight("Hi", 0, 2, 0, 0, fm); | ||
assertThat(fm.top).isEqualTo(-4); | ||
assertThat(fm.ascent).isEqualTo(-4); | ||
assertThat(fm.descent).isEqualTo(5); | ||
assertThat(fm.bottom).isEqualTo(5); | ||
} | ||
} |
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.
@bartolkaruza & @andreicoman11:
I've been looking at this PR and noticed a possible regression. Line height seems to be (more) broken on wrapped lines of text when line height is greater than font size. It seems to be related to this line of code.
Here is an example of what I'm seeing:
JSX:
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.
Restoring
fm.ascent -= additional;
gives me this behaviour: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.
Proposed fix to make Android consistent with iOS:
Giving following behaviour:
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.
@dickie81 Thank you for looking out, I did not check the full range of test cases on the actual UI when changing the style back to the original style, leaning on the unit tests instead. I'll try out your approach against the cases I have locally for all the lineHeight scenario's and post the test setup so everyone can verify.
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.
@dickie81 I've been testing the ui effects and there is definitely a regression in the case of bigger lineHeight and even with very small lineHeight something broke.
Unfortunately your proposed solution still causes cut-off at the bottom, when the lineHeight is small.
@andreicoman11 I'm looking into this more in depth, this PR is not ready yet as it stands now. I'll try to improve it to cover all scenario's.
Here is the testing setup that you can use to verify on your end: https://gist.github.com/bartolkaruza/b74f228636bb78fea00aa8bd82e3766d
And these are the 4 different implementations compared to iOS. Not one is quite right yet.
Current PR state after review changes:
Original implementation before PR:
Implementation before review changes:
Proposed change by @dickie81 :