Skip to content

Commit 5565bc1

Browse files
committed
adapt Superscript to top/bottom
#35704 (comment)
1 parent fdeb37c commit 5565bc1

4 files changed

Lines changed: 73 additions & 25 deletions

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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.SubscriptSpan;
12+
13+
/** ratio 0 for center ratio 0.4 for top ratio */
14+
public class ReactBottomAlignSpan extends SubscriptSpan implements ReactSpan {
15+
private static final String TAG = "ReactBottomAlignSpan";
16+
17+
@Override
18+
public void updateDrawState(TextPaint ds) {
19+
ds.baselineShift += ds.getFontMetrics().descent / 2;
20+
}
21+
22+
@Override
23+
public void updateMeasureState(TextPaint tp) {
24+
updateDrawState(tp);
25+
}
26+
}

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

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,21 @@
77

88
package com.facebook.react.views.text;
99

10+
import android.graphics.Rect;
1011
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-
}
12+
import android.text.style.SuperscriptSpan;
2513

14+
/** ratio 0 for center ratio 0.4 for top ratio */
15+
public class ReactTopAlignSpan extends SuperscriptSpan implements ReactSpan {
2616
@Override
27-
public void updateDrawState(TextPaint paint) {
28-
paint.baselineShift += (int) (paint.ascent() * ratio);
17+
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;
2921
}
3022

3123
@Override
32-
public void updateMeasureState(TextPaint paint) {
33-
paint.baselineShift += (int) (paint.ascent() * ratio);
24+
public void updateMeasureState(TextPaint tp) {
25+
updateDrawState(tp);
3426
}
3527
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,14 @@ private static void buildSpannableFromFragment(
140140
sb.length(),
141141
new TextInlineViewPlaceholderSpan(reactTag, (int) width, (int) height)));
142142
} else if (end >= start) {
143-
if (textAttributes.mVerticalAlign != null && textAttributes.mVerticalAlign.equals("top")) {
143+
if (textAttributes.mVerticalAlign != null
144+
&& textAttributes.mVerticalAlign.equals("top-child")) {
144145
ops.add(new SetSpanOperation(start, end, new ReactTopAlignSpan()));
145146
}
147+
if (textAttributes.mVerticalAlign != null
148+
&& textAttributes.mVerticalAlign.equals("bottom-child")) {
149+
ops.add(new SetSpanOperation(start, end, new ReactBottomAlignSpan()));
150+
}
146151
if (textAttributes.mIsAccessibilityLink) {
147152
ops.add(new SetSpanOperation(start, end, new ReactClickableSpan(reactTag)));
148153
}

‎packages/rn-tester/js/examples/Text/TextExample.android.js‎

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@ const React = require('react');
1818
const TextInlineView = require('../../components/TextInlineView');
1919
import TextLegend from '../../components/TextLegend';
2020

21-
const {LayoutAnimation, StyleSheet, Text, View} = require('react-native');
21+
const {
22+
Image,
23+
LayoutAnimation,
24+
StyleSheet,
25+
Text,
26+
View,
27+
} = require('react-native');
2228

2329
class Entity extends React.Component<{|children: React.Node|}> {
2430
render(): React.Node {
@@ -205,16 +211,35 @@ class TextExample extends React.Component<{...}> {
205211
return (
206212
<RNTesterPage title="<Text>">
207213
<RNTesterBlock title="Dynamic Font Size Adjustment">
208-
<Text>
209-
This text a{' '}
210-
<Text style={{textAlignVertical: 'top', backgroundColor: 'red'}}>
211-
SuperScript
214+
<Text
215+
onLayout={event => {
216+
console.log('height', event.nativeEvent.layout.height);
217+
const layout = event.nativeEvent.layout;
218+
console.log('layout:', layout);
219+
}}
220+
style={{
221+
textAlignVertical: 'bottom',
222+
backgroundColor: 'yellow',
223+
}}>
224+
A parent text with{' '}
225+
<Text
226+
style={{
227+
textAlignVertical: 'bottom-child',
228+
backgroundColor: 'red',
229+
}}>
230+
Top
231+
</Text>
232+
<Text
233+
style={{
234+
textAlignVertical: 'top-child',
235+
backgroundColor: 'green',
236+
}}>
237+
Bottom
212238
</Text>
213239
</Text>
214240
<Text
215241
style={{
216242
verticalAlign: 'bottom',
217-
height: 100,
218243
backgroundColor: 'red',
219244
}}>
220245
Bottom

0 commit comments

Comments
 (0)