Skip to content

Commit 2f7c405

Browse files
committed
Align text
1 parent 1db1dc5 commit 2f7c405

File tree

1 file changed

+30
-22
lines changed

1 file changed

+30
-22
lines changed

ReactAndroid/src/main/java/com/facebook/react/views/text/CustomLineHeightSpan.java

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,30 +21,38 @@ public CustomLineHeightSpan(float height) {
2121
this.mHeight = (int) Math.ceil(height);
2222
}
2323

24+
/**
25+
* Discord Story time!
26+
*
27+
* So since we decided to be _very_ flexible with channel names, users have decided that they were gonna name their channels
28+
* shit like ‧͙⁺˚・༓☾text☽༓・˚⁺‧͙ | l̶̟̦͚͎̦͑̎m̵̮̥̫͕͚̜̱̫̺̪͍̯̉̂̔͌́̚̕a̶͖̫͍͇̯̯̭͎͋̅́̿́̕͘͘͝͝ò̶̧̢͎̃̋͆̉͠ | and other fun non-standard channel names.
29+
*
30+
* This caused issues with line heights, because the RN implementation decided that it would try as best as possible to
31+
* fit the text within the lineHeight that was given to it by the react component, causing text to be shifted upward
32+
* and look terrible (see: https://canary.discord.com/channels/281683040739262465/912423796915462154/101286117376867126
33+
* for an example).
34+
*
35+
* We (Jerry + Charles) decided that to fix this issue, we would instead ignore lineHeights _only_ if the text
36+
* height was larger than the lineHeight provided to it.
37+
*
38+
* This is a much simpler implementation that what was previously here.
39+
*
40+
* _IF_ the lineHeight is larger than the text height, we default to centering the text as much as possible within
41+
* that line height.
42+
*/
2443
@Override
2544
public void chooseHeight(
26-
CharSequence text, int start, int end, int spanstartv, int v, Paint.FontMetricsInt fm) {
27-
// This is more complicated that I wanted it to be. You can find a good explanation of what the
28-
// FontMetrics mean here: http://stackoverflow.com/questions/27631736.
29-
// The general solution is that if there's not enough height to show the full line height, we
30-
// will prioritize in this order: descent, ascent, bottom, top
31-
32-
if (fm.descent > mHeight) {
33-
// Show as much descent as possible
34-
fm.bottom = fm.descent = Math.min(mHeight, fm.descent);
35-
fm.top = fm.ascent = 0;
36-
} else if (-fm.ascent + fm.descent > mHeight) {
37-
// Show all descent, and as much ascent as possible
38-
fm.bottom = fm.descent;
39-
fm.top = fm.ascent = -mHeight + fm.descent;
40-
} else if (-fm.ascent + fm.bottom > mHeight) {
41-
// Show all ascent, descent, as much bottom as possible
42-
fm.top = fm.ascent;
43-
fm.bottom = fm.ascent + mHeight;
44-
} else if (-fm.top + fm.bottom > mHeight) {
45-
// Show all ascent, descent, bottom, as much top as possible
46-
fm.top = fm.bottom - mHeight;
47-
} else {
45+
CharSequence text, int start, int end, int spanstartv, int v, Paint.FontMetricsInt fm) {
46+
47+
final int originHeight = fm.descent - fm.ascent;
48+
// If original height is not positive, do nothing.
49+
if (originHeight <= 0) {
50+
return;
51+
}
52+
53+
int realTextHeight = fm.bottom - fm.top;
54+
55+
if (mHeight >= realTextHeight) {
4856
// Show proportionally additional ascent / top & descent / bottom
4957
final int additional = mHeight - (-fm.top + fm.bottom);
5058

0 commit comments

Comments
 (0)