@@ -138,7 +138,7 @@ private static void apply(
138138 ds .setTypeface (typeface );
139139 ds .setSubpixelText (true );
140140
141- if (textAlignVertical == TextAlignVertical .CENTER ) {
141+ if (textAlignVertical == TextAlignVertical .CENTER || highestLineHeight == 0 ) {
142142 return ;
143143 }
144144
@@ -147,48 +147,27 @@ private static void apply(
147147 if (textSize > 0 ) {
148148 textPaintCopy .setTextSize (textSize );
149149 }
150- if (highestLineHeight == 0 ) {
151- // aligns the text by font metrics
152- // when lineHeight prop is missing
153- // https://stackoverflow.com/a/27631737/7295772
154- // top ------------- -10
155- // ascent ------------- -5
156- // baseline __my Text____ 0
157- // descent _____________ 2
158- // bottom _____________ 5
150+ if (textSize == highestFontSize ) {
151+ // aligns text vertically in the lineHeight
152+ // and adjust their position depending on the fontSize
159153 if (textAlignVertical == TextAlignVertical .TOP ) {
160- ds .baselineShift +=
161- textPaintCopy .getFontMetrics ().top - textPaintCopy .ascent () - textPaintCopy .descent ();
154+ ds .baselineShift -= highestLineHeight / 2 - textPaintCopy .getTextSize () / 2 ;
162155 }
163156 if (textAlignVertical == TextAlignVertical .BOTTOM ) {
164- ds .baselineShift += textPaintCopy .getFontMetrics ().bottom - textPaintCopy .descent ();
157+ ds .baselineShift +=
158+ highestLineHeight / 2 - textPaintCopy .getTextSize () / 2 - textPaintCopy .descent ();
165159 }
166- } else {
167- if (textSize == highestFontSize ) {
168- // aligns text vertically in the lineHeight
169- // and adjust their position depending on the fontSize
170- if (textAlignVertical == TextAlignVertical .TOP ) {
171- ds .baselineShift -= highestLineHeight / 2 - textPaintCopy .getTextSize () / 2 ;
172- }
173- if (textAlignVertical == TextAlignVertical .BOTTOM ) {
174- ds .baselineShift +=
175- highestLineHeight / 2 - textPaintCopy .getTextSize () / 2 - textPaintCopy .descent ();
176- }
177- } else if (highestFontSize != 0 && textSize < highestFontSize ) {
178- // aligns correctly text that has smaller font
179- if (textAlignVertical == TextAlignVertical .TOP ) {
180- ds .baselineShift -=
181- highestLineHeight / 2
182- - highestFontSize / 2
183- // smaller font aligns on the baseline of bigger font
184- // moves the baseline of text with smaller font up
185- // so it aligns on the top of the larger font
186- + (highestFontSize - textSize )
187- + (textPaintCopy .getFontMetrics ().top - textPaintCopy .ascent ());
188- }
189- if (textAlignVertical == TextAlignVertical .BOTTOM ) {
190- ds .baselineShift += highestLineHeight / 2 - highestFontSize / 2 - textPaintCopy .descent ();
191- }
160+ } else if (highestFontSize != 0 && textSize < highestFontSize ) {
161+ // aligns correctly text that has smaller font
162+ if (textAlignVertical == TextAlignVertical .TOP ) {
163+ ds .baselineShift -=
164+ highestLineHeight / 2
165+ - highestFontSize / 2
166+ + (highestFontSize - textSize )
167+ + (textPaintCopy .getFontMetrics ().top - textPaintCopy .ascent ());
168+ }
169+ if (textAlignVertical == TextAlignVertical .BOTTOM ) {
170+ ds .baselineShift += highestLineHeight / 2 - highestFontSize / 2 - textPaintCopy .descent ();
192171 }
193172 }
194173 }
0 commit comments