Skip to content

[RNA] Align text #25

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

Merged
merged 2 commits into from
Aug 30, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ public CustomLineHeightSpan(float height) {

@Override
public void chooseHeight(
CharSequence text, int start, int end, int spanstartv, int v, Paint.FontMetricsInt fm) {
CharSequence text, int start, int end, int spanstartv, int v, Paint.FontMetricsInt fm) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: should be 4 spaces

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kill me


if (OVERRIDE_LINE_HEIGHT) {
overrideLineHeight(fm);
return;
}

// This is more complicated that I wanted it to be. You can find a good explanation of what the
// FontMetrics mean here: http://stackoverflow.com/questions/27631736.
// The general solution is that if there's not enough height to show the full line height, we
Expand Down Expand Up @@ -56,4 +62,41 @@ public void chooseHeight(
fm.descent = fm.bottom;
}
}

private final static boolean OVERRIDE_LINE_HEIGHT = true;

/**
* Discord Story time!
*
* So since we decided to be _very_ flexible with channel names, users have decided that they were gonna name their channels
* shit like ‧͙⁺˚・༓☾text☽༓・˚⁺‧͙ | l̶̟̦͚͎̦͑̎m̵̮̥̫͕͚̜̱̫̺̪͍̯̉̂̔͌́̚̕a̶͖̫͍͇̯̯̭͎͋̅́̿́̕͘͘͝͝ò̶̧̢͎̃̋͆̉͠ | and other fun non-standard channel names.
*
* This caused issues with line heights, because the RN implementation decided that it would try as best as possible to
* fit the text within the lineHeight that was given to it by the react component, causing text to be shifted upward
* and look terrible (see: https://canary.discord.com/channels/281683040739262465/912423796915462154/101286117376867126
* for an example).
*
* We (Jerry + Charles) decided that to fix this issue, we would instead ignore lineHeights _only_ if the text
* height was larger than the lineHeight provided to it.
*
* This is a much simpler implementation that what was previously here.
*
* _IF_ the lineHeight is larger than the text height, we default to centering the text as much as possible within
* that line height.
*/
private void overrideLineHeight(Paint.FontMetricsInt fm) {
int realTextHeight = fm.bottom - fm.top;

if (mHeight >= realTextHeight) {
// Show proportionally additional ascent / top & descent / bottom
final int additional = mHeight - (-fm.top + fm.bottom);

// Round up for the negative values and down for the positive values (arbitrary choice)
// So that bottom - top equals additional even if it's an odd number.
fm.top -= Math.ceil(additional / 2.0f);
fm.bottom += Math.floor(additional / 2.0f);
fm.ascent = fm.top;
fm.descent = fm.bottom;
}
}
}