7
7
8
8
package com .facebook .react .views .text ;
9
9
10
- import android .annotation .TargetApi ;
11
10
import android .os .Build ;
12
11
import android .text .BoringLayout ;
13
12
import android .text .Layout ;
47
46
* <p>The class measures text in {@code <Text>} view and feeds native {@link TextView} using {@code
48
47
* Spannable} object constructed in superclass.
49
48
*/
50
- @ TargetApi (Build .VERSION_CODES .M )
51
49
public class ReactTextShadowNode extends ReactBaseTextShadowNode {
52
50
53
51
// It's important to pass the ANTI_ALIAS_FLAG flag to the constructor rather than setting it
@@ -148,7 +146,7 @@ public long measure(
148
146
}
149
147
}
150
148
151
- if (android . os . Build .VERSION .SDK_INT > android . os . Build .VERSION_CODES .Q ) {
149
+ if (Build .VERSION .SDK_INT > Build .VERSION_CODES .Q ) {
152
150
layoutWidth = (float ) Math .ceil (layoutWidth );
153
151
}
154
152
float layoutHeight = height ;
@@ -222,28 +220,22 @@ private Layout measureSpannedText(Spannable text, float width, YogaMeasureMode w
222
220
|| (!YogaConstants .isUndefined (desiredWidth ) && desiredWidth <= width ))) {
223
221
// Is used when the width is not known and the text is not boring, ie. if it contains
224
222
// unicode characters.
225
-
226
223
int hintWidth = (int ) Math .ceil (desiredWidth );
227
- if (Build .VERSION .SDK_INT < Build .VERSION_CODES .M ) {
228
- layout =
229
- new StaticLayout (text , textPaint , hintWidth , alignment , 1.f , 0.f , mIncludeFontPadding );
230
- } else {
231
- StaticLayout .Builder builder =
232
- StaticLayout .Builder .obtain (text , 0 , text .length (), textPaint , hintWidth )
233
- .setAlignment (alignment )
234
- .setLineSpacing (0.f , 1.f )
235
- .setIncludePad (mIncludeFontPadding )
236
- .setBreakStrategy (mTextBreakStrategy )
237
- .setHyphenationFrequency (mHyphenationFrequency );
238
-
239
- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ) {
240
- builder .setJustificationMode (mJustificationMode );
241
- }
242
- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .P ) {
243
- builder .setUseLineSpacingFromFallbacks (true );
244
- }
245
- layout = builder .build ();
224
+ StaticLayout .Builder builder =
225
+ StaticLayout .Builder .obtain (text , 0 , text .length (), textPaint , hintWidth )
226
+ .setAlignment (alignment )
227
+ .setLineSpacing (0.f , 1.f )
228
+ .setIncludePad (mIncludeFontPadding )
229
+ .setBreakStrategy (mTextBreakStrategy )
230
+ .setHyphenationFrequency (mHyphenationFrequency );
231
+
232
+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ) {
233
+ builder .setJustificationMode (mJustificationMode );
234
+ }
235
+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .P ) {
236
+ builder .setUseLineSpacingFromFallbacks (true );
246
237
}
238
+ layout = builder .build ();
247
239
248
240
} else if (boring != null && (unconstrainedWidth || boring .width <= width )) {
249
241
// Is used for single-line, boring text when the width is either unknown or bigger
@@ -260,32 +252,25 @@ private Layout measureSpannedText(Spannable text, float width, YogaMeasureMode w
260
252
mIncludeFontPadding );
261
253
} else {
262
254
// Is used for multiline, boring text and the width is known.
255
+ // Android 11+ introduces changes in text width calculation which leads to cases
256
+ // where the container is measured smaller than text. Math.ceil prevents it
257
+ // See T136756103 for investigation
258
+ if (Build .VERSION .SDK_INT > Build .VERSION_CODES .Q ) {
259
+ width = (float ) Math .ceil (width );
260
+ }
263
261
264
- if (Build .VERSION .SDK_INT < Build .VERSION_CODES .M ) {
265
- layout =
266
- new StaticLayout (
267
- text , textPaint , (int ) width , alignment , 1.f , 0.f , mIncludeFontPadding );
268
- } else {
269
- // Android 11+ introduces changes in text width calculation which leads to cases
270
- // where the container is measured smaller than text. Math.ceil prevents it
271
- // See T136756103 for investigation
272
- if (android .os .Build .VERSION .SDK_INT > android .os .Build .VERSION_CODES .Q ) {
273
- width = (float ) Math .ceil (width );
274
- }
275
-
276
- StaticLayout .Builder builder =
277
- StaticLayout .Builder .obtain (text , 0 , text .length (), textPaint , (int ) width )
278
- .setAlignment (alignment )
279
- .setLineSpacing (0.f , 1.f )
280
- .setIncludePad (mIncludeFontPadding )
281
- .setBreakStrategy (mTextBreakStrategy )
282
- .setHyphenationFrequency (mHyphenationFrequency );
262
+ StaticLayout .Builder builder =
263
+ StaticLayout .Builder .obtain (text , 0 , text .length (), textPaint , (int ) width )
264
+ .setAlignment (alignment )
265
+ .setLineSpacing (0.f , 1.f )
266
+ .setIncludePad (mIncludeFontPadding )
267
+ .setBreakStrategy (mTextBreakStrategy )
268
+ .setHyphenationFrequency (mHyphenationFrequency );
283
269
284
- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .P ) {
285
- builder .setUseLineSpacingFromFallbacks (true );
286
- }
287
- layout = builder .build ();
270
+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .P ) {
271
+ builder .setUseLineSpacingFromFallbacks (true );
288
272
}
273
+ layout = builder .build ();
289
274
}
290
275
return layout ;
291
276
}
@@ -361,6 +346,7 @@ public void setShouldNotifyOnTextLayout(boolean shouldNotifyOnTextLayout) {
361
346
}
362
347
363
348
@ Override
349
+ @ Nullable
364
350
public Iterable <? extends ReactShadowNode > calculateLayoutOnChildren () {
365
351
// Run flexbox on and return the descendants which are inline views.
366
352
@@ -374,7 +360,7 @@ public Iterable<? extends ReactShadowNode> calculateLayoutOnChildren() {
374
360
"Spannable element has not been prepared in onBeforeLayout" );
375
361
TextInlineViewPlaceholderSpan [] placeholders =
376
362
text .getSpans (0 , text .length (), TextInlineViewPlaceholderSpan .class );
377
- ArrayList <ReactShadowNode > shadowNodes = new ArrayList <ReactShadowNode >(placeholders .length );
363
+ ArrayList <ReactShadowNode > shadowNodes = new ArrayList <>(placeholders .length );
378
364
379
365
for (TextInlineViewPlaceholderSpan placeholder : placeholders ) {
380
366
ReactShadowNode child = mInlineViews .get (placeholder .getReactTag ());
0 commit comments