Skip to content

Commit 72ef53a

Browse files
authored
Merge pull request #25 from discord/charles/rna-text-align
[RNA] Align text
2 parents 1db1dc5 + 7a6a650 commit 72ef53a

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ public CustomLineHeightSpan(float height) {
2424
@Override
2525
public void chooseHeight(
2626
CharSequence text, int start, int end, int spanstartv, int v, Paint.FontMetricsInt fm) {
27+
28+
if (OVERRIDE_LINE_HEIGHT) {
29+
overrideLineHeight(fm);
30+
return;
31+
}
32+
2733
// This is more complicated that I wanted it to be. You can find a good explanation of what the
2834
// FontMetrics mean here: http://stackoverflow.com/questions/27631736.
2935
// The general solution is that if there's not enough height to show the full line height, we
@@ -56,4 +62,41 @@ public void chooseHeight(
5662
fm.descent = fm.bottom;
5763
}
5864
}
65+
66+
private final static boolean OVERRIDE_LINE_HEIGHT = true;
67+
68+
/**
69+
* Discord Story time!
70+
*
71+
* So since we decided to be _very_ flexible with channel names, users have decided that they were gonna name their channels
72+
* shit like ‧͙⁺˚・༓☾text☽༓・˚⁺‧͙ | l̶̟̦͚͎̦͑̎m̵̮̥̫͕͚̜̱̫̺̪͍̯̉̂̔͌́̚̕a̶͖̫͍͇̯̯̭͎͋̅́̿́̕͘͘͝͝ò̶̧̢͎̃̋͆̉͠ | and other fun non-standard channel names.
73+
*
74+
* This caused issues with line heights, because the RN implementation decided that it would try as best as possible to
75+
* fit the text within the lineHeight that was given to it by the react component, causing text to be shifted upward
76+
* and look terrible (see: https://canary.discord.com/channels/281683040739262465/912423796915462154/101286117376867126
77+
* for an example).
78+
*
79+
* We (Jerry + Charles) decided that to fix this issue, we would instead ignore lineHeights _only_ if the text
80+
* height was larger than the lineHeight provided to it.
81+
*
82+
* This is a much simpler implementation that what was previously here.
83+
*
84+
* _IF_ the lineHeight is larger than the text height, we default to centering the text as much as possible within
85+
* that line height.
86+
*/
87+
private void overrideLineHeight(Paint.FontMetricsInt fm) {
88+
int realTextHeight = fm.bottom - fm.top;
89+
90+
if (mHeight >= realTextHeight) {
91+
// Show proportionally additional ascent / top & descent / bottom
92+
final int additional = mHeight - (-fm.top + fm.bottom);
93+
94+
// Round up for the negative values and down for the positive values (arbitrary choice)
95+
// So that bottom - top equals additional even if it's an odd number.
96+
fm.top -= Math.ceil(additional / 2.0f);
97+
fm.bottom += Math.floor(additional / 2.0f);
98+
fm.ascent = fm.top;
99+
fm.descent = fm.bottom;
100+
}
101+
}
59102
}

0 commit comments

Comments
 (0)