Minimize EditText Spans 6/9: letterSpacing (#36548)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/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]( https://github.com/facebook/react-native/issues/35936#issuecomment-1411437789) 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
This commit is contained in:
Nick Gerleman
2023-03-24 12:31:22 -07:00
committed by Lorenzo Sciandra
parent 44a96acc20
commit 64eeb819b9
2 changed files with 26 additions and 1 deletions
@@ -37,6 +37,10 @@ public class CustomLetterSpacingSpan extends MetricAffectingSpan implements Reac
apply(paint);
}
public float getSpacing() {
return mLetterSpacing;
}
private void apply(TextPaint paint) {
if (!Float.isNaN(mLetterSpacing)) {
paint.setLetterSpacing(mLetterSpacing);
@@ -691,6 +691,18 @@ public class ReactEditText extends AppCompatEditText
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(
@@ -732,6 +744,13 @@ public class ReactEditText extends AppCompatEditText
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);
}
@@ -1083,7 +1102,9 @@ public class ReactEditText extends AppCompatEditText
float effectiveLetterSpacing = mTextAttributes.getEffectiveLetterSpacing();
if (!Float.isNaN(effectiveLetterSpacing)) {
setLetterSpacing(effectiveLetterSpacing);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
setLetterSpacing(effectiveLetterSpacing);
}
}
}