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: 1aa4c7227cc43d8794534348956bd4364ba03fd7
  • Loading branch information
NickGerleman authored and facebook-github-bot committed Mar 22, 2023
1 parent bdf5dd4 commit 0896219
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 @@ -780,6 +792,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 @@ -1137,7 +1156,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 0896219

Please sign in to comment.