Skip to content

Commit fdeb37c

Browse files
committed
Modify Superscript implementation to align to the top (stackoverflow-1, stackoverflow-2)
#35704 (comment)
1 parent 7914cfd commit fdeb37c

File tree

3 files changed

+36
-17
lines changed

3 files changed

+36
-17
lines changed

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

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
package com.facebook.react.views.text;
9+
10+
import android.text.TextPaint;
11+
import android.text.style.MetricAffectingSpan;
12+
13+
/**
14+
* Instances of this class are used to place reactTag information of nested text react nodes into
15+
* spannable text rendered by single {@link TextView}
16+
*/
17+
public class ReactTopAlignSpan extends MetricAffectingSpan implements ReactSpan {
18+
double ratio = 0.4;
19+
20+
public ReactTopAlignSpan() {}
21+
22+
public ReactTopAlignSpan(double ratio) {
23+
this.ratio = ratio;
24+
}
25+
26+
@Override
27+
public void updateDrawState(TextPaint paint) {
28+
paint.baselineShift += (int) (paint.ascent() * ratio);
29+
}
30+
31+
@Override
32+
public void updateMeasureState(TextPaint paint) {
33+
paint.baselineShift += (int) (paint.ascent() * ratio);
34+
}
35+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ private static void buildSpannableFromFragment(
141141
new TextInlineViewPlaceholderSpan(reactTag, (int) width, (int) height)));
142142
} else if (end >= start) {
143143
if (textAttributes.mVerticalAlign != null && textAttributes.mVerticalAlign.equals("top")) {
144-
ops.add(new SetSpanOperation(start, end, new ReactSuperscriptSpan()));
144+
ops.add(new SetSpanOperation(start, end, new ReactTopAlignSpan()));
145145
}
146146
if (textAttributes.mIsAccessibilityLink) {
147147
ops.add(new SetSpanOperation(start, end, new ReactClickableSpan(reactTag)));

0 commit comments

Comments
 (0)