@@ -92,21 +92,6 @@ public static MountableLayoutResult layout(
92
92
}
93
93
94
94
Pair <Rect , TextLayout > result = layout (androidContext , widthSpec , heightSpec , text , textStyle );
95
- final boolean fitTextToConstraints =
96
- textStyle .shouldTruncateTextUsingConstraints
97
- && textStyle .maxLines == Integer .MAX_VALUE
98
- && View .MeasureSpec .getMode (heightSpec ) != View .MeasureSpec .UNSPECIFIED ;
99
- if (fitTextToConstraints ) {
100
- final int measuredHeightConstraint = result .first .height ();
101
- final boolean isSizeCompatible =
102
- LayoutMeasureUtil .getHeight (result .second .layout ) <= measuredHeightConstraint ;
103
- if (!isSizeCompatible ) {
104
- result =
105
- maybeFitTextToConstraints (
106
- androidContext , heightSpec , widthSpec , textStyle , text , result );
107
- }
108
- }
109
-
110
95
if (textStyle .roundedBackgroundProps != null && text instanceof Spannable ) {
111
96
result =
112
97
calculateLayoutWithBackgroundSpan (
@@ -117,39 +102,6 @@ public static MountableLayoutResult layout(
117
102
renderUnit , result .first .width (), result .first .height (), result .second );
118
103
}
119
104
120
- private static Pair <Rect , TextLayout > maybeFitTextToConstraints (
121
- Context context ,
122
- int heightSpec ,
123
- int widthSpec ,
124
- TextStyle textStyle ,
125
- CharSequence text ,
126
- Pair <Rect , TextLayout > initialResult ) {
127
- final Layout layout = initialResult .second .layout ;
128
-
129
- final int measuredHeightConstraint = initialResult .first .height ();
130
-
131
- // Make sure we have the bare minimum line count if we've exhausted all lines and weren't able
132
- // to fit. This allows to show the ellipsis in the best case scenario signaling truncation
133
- int linesWithinConstrainedBounds = 1 ;
134
- int lineIndex = layout .getLineCount () - 1 ;
135
- while (lineIndex >= 0 ) {
136
- if (layout .getLineBottom (lineIndex ) <= measuredHeightConstraint ) {
137
- linesWithinConstrainedBounds = lineIndex + 1 ;
138
- break ;
139
- }
140
-
141
- lineIndex -= 1 ;
142
- }
143
-
144
- if (layout .getLineCount () > linesWithinConstrainedBounds ) {
145
- textStyle .setMaxLines (linesWithinConstrainedBounds );
146
- textStyle .setEllipsize (TextUtils .TruncateAt .END );
147
- return layout (context , widthSpec , heightSpec , text , textStyle );
148
- }
149
-
150
- return initialResult ;
151
- }
152
-
153
105
public static Pair <Rect , TextLayout > calculateLayoutWithBackgroundSpan (
154
106
final Context context ,
155
107
final Pair <Rect , TextLayout > result ,
@@ -259,6 +211,37 @@ public static Pair<Rect, TextLayout> layout(
259
211
TextMeasurementUtils .createTextLayout (
260
212
context , textStyle , widthSpec , heightSpec , processedText );
261
213
214
+ // check if the layout should truncate based on the size constraints
215
+ int linesWithinConstrainedBounds = -1 ;
216
+ if (View .MeasureSpec .getMode (heightSpec ) != View .MeasureSpec .UNSPECIFIED ) {
217
+ final int heightConstraint = View .MeasureSpec .getSize (heightSpec );
218
+ final boolean fitTextToConstraints =
219
+ textStyle .shouldTruncateTextUsingConstraints
220
+ && textStyle .maxLines == Integer .MAX_VALUE
221
+ && LayoutMeasureUtil .getHeight (layout ) > heightConstraint ;
222
+
223
+ if (fitTextToConstraints ) {
224
+ linesWithinConstrainedBounds = 1 ;
225
+ int lineIndex = layout .getLineCount () - 1 ;
226
+ while (lineIndex >= 0 ) {
227
+ if (layout .getLineBottom (lineIndex ) <= heightConstraint ) {
228
+ linesWithinConstrainedBounds = lineIndex + 1 ;
229
+ break ;
230
+ }
231
+
232
+ lineIndex -= 1 ;
233
+ }
234
+ }
235
+ }
236
+
237
+ if (linesWithinConstrainedBounds != -1 ) {
238
+ // we have constrained the number of lines that can fit, so truncate the layout
239
+ textStyle .setMaxLines (linesWithinConstrainedBounds );
240
+ layout =
241
+ TextMeasurementUtils .createTextLayout (
242
+ context , textStyle , widthSpec , heightSpec , processedText );
243
+ }
244
+
262
245
final int layoutWidth =
263
246
View .resolveSize (
264
247
layout .getWidth ()
0 commit comments