From d8556a7b4c32399636f476437b626fb4fae4055c Mon Sep 17 00:00:00 2001 From: fabriziobertoglio1987 Date: Thu, 9 Feb 2023 23:26:36 +0100 Subject: [PATCH] The parent Text does not align correctly with CustomStyleSpan with Gravity.TOP (correct-behaviour, not-correct-behaviour, commit) https://github.com/facebook/react-native/pull/35949#issuecomment-1424918182 --- .../react/views/text/CustomStyleSpan.java | 57 ++++++------------- 1 file changed, 18 insertions(+), 39 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/text/CustomStyleSpan.java b/ReactAndroid/src/main/java/com/facebook/react/views/text/CustomStyleSpan.java index 5e202266f60179..02d704fedc5887 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/text/CustomStyleSpan.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/text/CustomStyleSpan.java @@ -138,7 +138,7 @@ private static void apply( ds.setTypeface(typeface); ds.setSubpixelText(true); - if (textAlignVertical == TextAlignVertical.CENTER) { + if (textAlignVertical == TextAlignVertical.CENTER || highestLineHeight == 0) { return; } @@ -147,48 +147,27 @@ private static void apply( if (textSize > 0) { textPaintCopy.setTextSize(textSize); } - if (highestLineHeight == 0) { - // aligns the text by font metrics - // when lineHeight prop is missing - // https://stackoverflow.com/a/27631737/7295772 - // top ------------- -10 - // ascent ------------- -5 - // baseline __my Text____ 0 - // descent _____________ 2 - // bottom _____________ 5 + if (textSize == highestFontSize) { + // aligns text vertically in the lineHeight + // and adjust their position depending on the fontSize if (textAlignVertical == TextAlignVertical.TOP) { - ds.baselineShift += - textPaintCopy.getFontMetrics().top - textPaintCopy.ascent() - textPaintCopy.descent(); + ds.baselineShift -= highestLineHeight / 2 - textPaintCopy.getTextSize() / 2; } if (textAlignVertical == TextAlignVertical.BOTTOM) { - ds.baselineShift += textPaintCopy.getFontMetrics().bottom - textPaintCopy.descent(); + ds.baselineShift += + highestLineHeight / 2 - textPaintCopy.getTextSize() / 2 - textPaintCopy.descent(); } - } else { - if (textSize == highestFontSize) { - // aligns text vertically in the lineHeight - // and adjust their position depending on the fontSize - if (textAlignVertical == TextAlignVertical.TOP) { - ds.baselineShift -= highestLineHeight / 2 - textPaintCopy.getTextSize() / 2; - } - if (textAlignVertical == TextAlignVertical.BOTTOM) { - ds.baselineShift += - highestLineHeight / 2 - textPaintCopy.getTextSize() / 2 - textPaintCopy.descent(); - } - } else if (highestFontSize != 0 && textSize < highestFontSize) { - // aligns correctly text that has smaller font - if (textAlignVertical == TextAlignVertical.TOP) { - ds.baselineShift -= - highestLineHeight / 2 - - highestFontSize / 2 - // smaller font aligns on the baseline of bigger font - // moves the baseline of text with smaller font up - // so it aligns on the top of the larger font - + (highestFontSize - textSize) - + (textPaintCopy.getFontMetrics().top - textPaintCopy.ascent()); - } - if (textAlignVertical == TextAlignVertical.BOTTOM) { - ds.baselineShift += highestLineHeight / 2 - highestFontSize / 2 - textPaintCopy.descent(); - } + } else if (highestFontSize != 0 && textSize < highestFontSize) { + // aligns correctly text that has smaller font + if (textAlignVertical == TextAlignVertical.TOP) { + ds.baselineShift -= + highestLineHeight / 2 + - highestFontSize / 2 + + (highestFontSize - textSize) + + (textPaintCopy.getFontMetrics().top - textPaintCopy.ascent()); + } + if (textAlignVertical == TextAlignVertical.BOTTOM) { + ds.baselineShift += highestLineHeight / 2 - highestFontSize / 2 - textPaintCopy.descent(); } } }