From 11b9bad5b4839f75026761f71f8c7d57c6628b09 Mon Sep 17 00:00:00 2001 From: David Vacca Date: Thu, 7 May 2020 17:08:17 -0700 Subject: [PATCH] Small refactor on text measure method Summary: Quick refactor of TextLayoutManager class changelog: [Internal] Reviewed By: shergin Differential Revision: D21446736 fbshipit-source-id: a32bdf534b167e128c8c0054cf6a126131fa740a --- .../react/views/text/TextLayoutManager.java | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/text/TextLayoutManager.java b/ReactAndroid/src/main/java/com/facebook/react/views/text/TextLayoutManager.java index 76176981b55..f98035c179b 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/text/TextLayoutManager.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/text/TextLayoutManager.java @@ -47,6 +47,9 @@ public class TextLayoutManager { private static final Object sSpannableCacheLock = new Object(); private static final boolean DEFAULT_INCLUDE_FONT_PADDING = true; + private static final String INCLUDE_FONT_PADDING_KEY = "includeFontPadding"; + private static final String TEXT_BREAK_STRATEGY_KEY = "textBreakStrategy"; + private static final String MAXIMUM_NUMBER_OF_LINES_KEY = "maximumNumberOfLines"; private static LruCache sSpannableCache = new LruCache<>(spannableCacheSize); public static boolean isRTL(ReadableMap attributedString) { @@ -217,20 +220,20 @@ public class TextLayoutManager { // TODO(5578671): Handle text direction (see View#getTextDirectionHeuristic) TextPaint textPaint = sTextPaintInstance; - Spannable preparedSpannableText = + Spannable text = getOrCreateSpannableForText(context, attributedString, reactTextViewManagerCallback); int textBreakStrategy = - TextAttributeProps.getTextBreakStrategy(paragraphAttributes.getString("textBreakStrategy")); + TextAttributeProps.getTextBreakStrategy( + paragraphAttributes.getString(TEXT_BREAK_STRATEGY_KEY)); boolean includeFontPadding = - paragraphAttributes.hasKey("includeFontPadding") - ? paragraphAttributes.getBoolean("includeFontPadding") + paragraphAttributes.hasKey(INCLUDE_FONT_PADDING_KEY) + ? paragraphAttributes.getBoolean(INCLUDE_FONT_PADDING_KEY) : DEFAULT_INCLUDE_FONT_PADDING; - if (preparedSpannableText == null) { + if (text == null) { throw new IllegalStateException("Spannable element has not been prepared in onBeforeLayout"); } - Spanned text = preparedSpannableText; BoringLayout.Metrics boring = BoringLayout.isBoring(text, textPaint); float desiredWidth = boring == null ? Layout.getDesiredWidth(text, textPaint) : Float.NaN; @@ -306,8 +309,8 @@ public class TextLayoutManager { } int maximumNumberOfLines = - paragraphAttributes.hasKey("maximumNumberOfLines") - ? paragraphAttributes.getInt("maximumNumberOfLines") + paragraphAttributes.hasKey(MAXIMUM_NUMBER_OF_LINES_KEY) + ? paragraphAttributes.getInt(MAXIMUM_NUMBER_OF_LINES_KEY) : UNSET; int calculatedLineCount =