Skip to content

Commit 9de465b

Browse files
committed
Update TopAlignSpan baseline position from ReactTextView#onLayout with the updated TextView height
#35704 (comment)
1 parent 5565bc1 commit 9de465b

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTextView.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,10 @@ public class ReactTextView extends AppCompatTextView implements ReactCompoundVie
5050

5151
private static final ViewGroup.LayoutParams EMPTY_LAYOUT_PARAMS =
5252
new ViewGroup.LayoutParams(0, 0);
53+
private static final String TAG = "ReactTextView";
5354

5455
private boolean mContainsImages;
56+
private boolean mContainsSpans;
5557
private final int mDefaultGravityHorizontal;
5658
private final int mDefaultGravityVertical;
5759
private int mTextAlign;
@@ -182,6 +184,16 @@ private ReactContext getReactContext() {
182184
protected void onLayout(
183185
boolean changed, int textViewLeft, int textViewTop, int textViewRight, int textViewBottom) {
184186
// TODO T62882314: Delete this method when Fabric is fully released in OSS
187+
if (getText() instanceof Spanned) {
188+
Spanned text = (Spanned) getText();
189+
ReactTopAlignSpan[] spans = text.getSpans(0, text.length(), ReactTopAlignSpan.class);
190+
if (spans.length != 0) {
191+
for (ReactTopAlignSpan span : spans) {
192+
span.setHeight(getHeight());
193+
invalidate();
194+
}
195+
}
196+
}
185197
int reactTag = getId();
186198
if (!(getText() instanceof Spanned)
187199
|| ViewUtil.getUIManagerType(reactTag) == UIManagerType.FABRIC) {

ReactAndroid/src/main/java/com/facebook/react/views/text/ReactTopAlignSpan.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,18 @@
1313

1414
/** ratio 0 for center ratio 0.4 for top ratio */
1515
public class ReactTopAlignSpan extends SuperscriptSpan implements ReactSpan {
16+
private static final String TAG = "ReactTopAlignSpan";
17+
private Integer mHeight;
18+
1619
@Override
1720
public void updateDrawState(TextPaint ds) {
18-
Rect bounds = new Rect();
19-
ds.getTextBounds("1A", 0, 2, bounds);
20-
ds.baselineShift -= bounds.top - ds.getFontMetrics().ascent;
21+
if (mHeight != null) {
22+
ds.baselineShift -= mHeight - ds.getTextSize();
23+
}
24+
}
25+
26+
public void setHeight(Integer height) {
27+
mHeight = height;
2128
}
2229

2330
@Override

0 commit comments

Comments
 (0)