Skip to content

Commit

Permalink
Minimize EditText Spans 6/9: letterSpacing (#36548)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #36548

This is part of a series of changes to minimize the number of spans committed to EditText, as a mitigation for platform issues on Samsung devices. See this [GitHub thread]( #35936 (comment)) for greater context on the platform behavior.

This change lets us set `letterSpacing` on the EditText instead of using our custom span.

Changelog:
[Android][Fixed] - Minimize EditText Spans 6/N: letterSpacing

Reviewed By: rshest

Differential Revision: D44240777

fbshipit-source-id: 9bd10c3261257037d8cacf37971011aaa94d1a77
  • Loading branch information
NickGerleman authored and facebook-github-bot committed Mar 24, 2023
1 parent 0869ea2 commit 5791cf1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ public void updateMeasureState(TextPaint paint) {
apply(paint);
}

public float getSpacing() {
return mLetterSpacing;
}

private void apply(TextPaint paint) {
if (!Float.isNaN(mLetterSpacing)) {
paint.setLetterSpacing(mLetterSpacing);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,18 @@ public boolean test(ReactUnderlineSpan span) {
return (getPaintFlags() & Paint.UNDERLINE_TEXT_FLAG) != 0;
}
});

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
stripSpansOfKind(
sb,
CustomLetterSpacingSpan.class,
new SpanPredicate<CustomLetterSpacingSpan>() {
@Override
public boolean test(CustomLetterSpacingSpan span) {
return span.getSpacing() == mTextAttributes.getEffectiveLetterSpacing();
}
});
}
}

private <T> void stripSpansOfKind(
Expand Down Expand Up @@ -767,6 +779,13 @@ private void restoreStyleEquivalentSpans(SpannableStringBuilder workingText) {
spans.add(new ReactUnderlineSpan());
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
float effectiveLetterSpacing = mTextAttributes.getEffectiveLetterSpacing();
if (!Float.isNaN(effectiveLetterSpacing)) {
spans.add(new CustomLetterSpacingSpan(effectiveLetterSpacing));
}
}

for (Object span : spans) {
workingText.setSpan(span, 0, workingText.length(), spanFlags);
}
Expand Down Expand Up @@ -1124,7 +1143,9 @@ protected void applyTextAttributes() {

float effectiveLetterSpacing = mTextAttributes.getEffectiveLetterSpacing();
if (!Float.isNaN(effectiveLetterSpacing)) {
setLetterSpacing(effectiveLetterSpacing);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
setLetterSpacing(effectiveLetterSpacing);
}
}
}

Expand Down

0 comments on commit 5791cf1

Please sign in to comment.