Skip to content

Commit

Permalink
[TopAppBar] Correctly set letter spacing when expanding titles
Browse files Browse the repository at this point in the history
We recreated title text layout when expanding from the collapsed state but didn't correctly set the letter spacing to the expanded one. The letter spacing being used will be a interpolated value between collapsed and expanded letter spacing (close to the collapsed one due to the implementation). This will cause the text layout incorrectly calculate the line count from the wrong letter spacing and result in a broken layout.

Correctly sets the letter spacing to fix the issue.

Resolves #2463

PiperOrigin-RevId: 408910555
  • Loading branch information
drchen committed Nov 10, 2021
1 parent 34cce18 commit 44d4c3e
Showing 1 changed file with 11 additions and 1 deletion.
Expand Up @@ -153,6 +153,7 @@ public final class CollapsingTextHelper {

private float collapsedLetterSpacing;
private float expandedLetterSpacing;
private float currentLetterSpacing;

private StaticLayout textLayout;
private float collapsedTextWidth;
Expand Down Expand Up @@ -907,10 +908,12 @@ private void calculateUsingTextSize(final float textSize, boolean forceRecalcula

float availableWidth;
float newTextSize;
float newLetterSpacing;
boolean updateDrawText = false;

if (isClose(textSize, collapsedTextSize)) {
newTextSize = collapsedTextSize;
newLetterSpacing = collapsedLetterSpacing;
scale = 1f;
if (currentTypeface != collapsedTypeface) {
currentTypeface = collapsedTypeface;
Expand All @@ -919,6 +922,7 @@ private void calculateUsingTextSize(final float textSize, boolean forceRecalcula
availableWidth = collapsedWidth;
} else {
newTextSize = expandedTextSize;
newLetterSpacing = expandedLetterSpacing;
if (currentTypeface != expandedTypeface) {
currentTypeface = expandedTypeface;
updateDrawText = true;
Expand Down Expand Up @@ -953,14 +957,20 @@ private void calculateUsingTextSize(final float textSize, boolean forceRecalcula
}

if (availableWidth > 0) {
updateDrawText = (currentTextSize != newTextSize) || boundsChanged || updateDrawText;
boolean textSizeChanged = currentTextSize != newTextSize;
boolean letterSpacingChanged = currentLetterSpacing != newLetterSpacing;
updateDrawText = textSizeChanged || letterSpacingChanged || boundsChanged || updateDrawText;
currentTextSize = newTextSize;
currentLetterSpacing = newLetterSpacing;
boundsChanged = false;
}

if (textToDraw == null || updateDrawText) {
textPaint.setTextSize(currentTextSize);
textPaint.setTypeface(currentTypeface);
if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
textPaint.setLetterSpacing(currentLetterSpacing);
}
// Use linear text scaling if we're scaling the canvas
textPaint.setLinearText(scale != 1f);

Expand Down

0 comments on commit 44d4c3e

Please sign in to comment.