Skip to content

Commit f0ce498

Browse files
dsn5ftleticiarossi
authored andcommitted
[CollapsingToolbarLayout] Added title line spacing and hyphenation frequency setters
PiperOrigin-RevId: 371920600
1 parent 401a5a8 commit f0ce498

File tree

3 files changed

+135
-1
lines changed

3 files changed

+135
-1
lines changed

lib/java/com/google/android/material/appbar/CollapsingToolbarLayout.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import android.widget.FrameLayout;
4848
import androidx.annotation.ColorInt;
4949
import androidx.annotation.DrawableRes;
50+
import androidx.annotation.FloatRange;
5051
import androidx.annotation.IntDef;
5152
import androidx.annotation.IntRange;
5253
import androidx.annotation.NonNull;
@@ -1239,6 +1240,57 @@ public int getLineCount() {
12391240
return collapsingTextHelper.getLineCount();
12401241
}
12411242

1243+
/**
1244+
* Sets the line spacing addition of the title text. See {@link
1245+
* android.widget.TextView#setLineSpacing(float, float)}. Experimental Feature.
1246+
*/
1247+
@RestrictTo(LIBRARY_GROUP)
1248+
@RequiresApi(VERSION_CODES.M)
1249+
public void setLineSpacingAdd(float spacingAdd) {
1250+
collapsingTextHelper.setLineSpacingAdd(spacingAdd);
1251+
}
1252+
1253+
/** Gets the line spacing addition of the title text, or -1 if not set. Experimental Feature. */
1254+
@RestrictTo(LIBRARY_GROUP)
1255+
@RequiresApi(VERSION_CODES.M)
1256+
public float getLineSpacingAdd() {
1257+
return collapsingTextHelper.getLineSpacingAdd();
1258+
}
1259+
1260+
/**
1261+
* Sets the line spacing multiplier of the title text. See {@link
1262+
* android.widget.TextView#setLineSpacing(float, float)}. Experimental Feature.
1263+
*/
1264+
@RestrictTo(LIBRARY_GROUP)
1265+
@RequiresApi(VERSION_CODES.M)
1266+
public void setLineSpacingMultiplier(@FloatRange(from = 0.0) float spacingMultiplier) {
1267+
collapsingTextHelper.setLineSpacingMultiplier(spacingMultiplier);
1268+
}
1269+
1270+
/** Gets the line spacing multiplier of the title text, or -1 if not set. Experimental Feature. */
1271+
@RestrictTo(LIBRARY_GROUP)
1272+
@RequiresApi(VERSION_CODES.M)
1273+
public float getLineSpacingMultiplier() {
1274+
return collapsingTextHelper.getLineSpacingMultiplier();
1275+
}
1276+
1277+
/**
1278+
* Sets the hyphenation frequency of the title text. See {@link
1279+
* android.widget.TextView#setHyphenationFrequency(int)}. Experimental Feature.
1280+
*/
1281+
@RestrictTo(LIBRARY_GROUP)
1282+
@RequiresApi(VERSION_CODES.M)
1283+
public void setHyphenationFrequency(int hyphenationFrequency) {
1284+
collapsingTextHelper.setHyphenationFrequency(hyphenationFrequency);
1285+
}
1286+
1287+
/** Gets the hyphenation frequency of the title text, or -1 if not set. Experimental Feature. */
1288+
@RestrictTo(LIBRARY_GROUP)
1289+
@RequiresApi(VERSION_CODES.M)
1290+
public int getHyphenationFrequency() {
1291+
return collapsingTextHelper.getHyphenationFrequency();
1292+
}
1293+
12421294
/**
12431295
* Set the amount of visible height in pixels used to define when to trigger a scrim visibility
12441296
* change.

lib/java/com/google/android/material/internal/CollapsingTextHelper.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import androidx.annotation.FloatRange;
4747
import androidx.annotation.NonNull;
4848
import androidx.annotation.Nullable;
49+
import androidx.annotation.RequiresApi;
4950
import androidx.annotation.RestrictTo;
5051
import androidx.core.math.MathUtils;
5152
import androidx.core.text.TextDirectionHeuristicsCompat;
@@ -153,6 +154,9 @@ public final class CollapsingTextHelper {
153154
private float expandedFirstLineDrawX;
154155
private CharSequence textToDrawCollapsed;
155156
private int maxLines = 1;
157+
private float lineSpacingAdd = StaticLayoutBuilderCompat.DEFAULT_LINE_SPACING_ADD;
158+
private float lineSpacingMultiplier = StaticLayoutBuilderCompat.DEFAULT_LINE_SPACING_MULTIPLIER;
159+
private int hyphenationFrequency = StaticLayoutBuilderCompat.DEFAULT_HYPHENATION_FREQUENCY;
156160

157161
public CollapsingTextHelper(View view) {
158162
this.view = view;
@@ -944,6 +948,8 @@ private StaticLayout createStaticLayout(int maxLines, float availableWidth, bool
944948
.setAlignment(ALIGN_NORMAL)
945949
.setIncludePad(false)
946950
.setMaxLines(maxLines)
951+
.setLineSpacing(lineSpacingAdd, lineSpacingMultiplier)
952+
.setHyphenationFrequency(hyphenationFrequency)
947953
.build();
948954
} catch (StaticLayoutBuilderCompatException e) {
949955
Log.e(TAG, e.getCause().getMessage(), e);
@@ -1026,6 +1032,36 @@ public int getLineCount() {
10261032
return textLayout != null ? textLayout.getLineCount() : 0;
10271033
}
10281034

1035+
@RequiresApi(VERSION_CODES.M)
1036+
public void setLineSpacingAdd(float spacingAdd) {
1037+
this.lineSpacingAdd = spacingAdd;
1038+
}
1039+
1040+
@RequiresApi(VERSION_CODES.M)
1041+
public float getLineSpacingAdd() {
1042+
return textLayout.getSpacingAdd();
1043+
}
1044+
1045+
@RequiresApi(VERSION_CODES.M)
1046+
public void setLineSpacingMultiplier(@FloatRange(from = 0.0) float spacingMultiplier) {
1047+
this.lineSpacingMultiplier = spacingMultiplier;
1048+
}
1049+
1050+
@RequiresApi(VERSION_CODES.M)
1051+
public float getLineSpacingMultiplier() {
1052+
return textLayout.getSpacingMultiplier();
1053+
}
1054+
1055+
@RequiresApi(VERSION_CODES.M)
1056+
public void setHyphenationFrequency(int hyphenationFrequency) {
1057+
this.hyphenationFrequency = hyphenationFrequency;
1058+
}
1059+
1060+
@RequiresApi(VERSION_CODES.M)
1061+
public int getHyphenationFrequency() {
1062+
return hyphenationFrequency;
1063+
}
1064+
10291065
/**
10301066
* Returns true if {@code value} is 'close' to it's closest decimal value. Close is currently
10311067
* defined as it's difference being < 0.001.

lib/java/com/google/android/material/internal/StaticLayoutBuilderCompat.java

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static androidx.core.util.Preconditions.checkNotNull;
2020

2121
import android.os.Build;
22+
import android.os.Build.VERSION;
2223
import android.os.Build.VERSION_CODES;
2324
import android.text.Layout;
2425
import android.text.Layout.Alignment;
@@ -52,6 +53,13 @@
5253
@RestrictTo(Scope.LIBRARY_GROUP)
5354
final class StaticLayoutBuilderCompat {
5455

56+
static final int DEFAULT_HYPHENATION_FREQUENCY =
57+
VERSION.SDK_INT >= VERSION_CODES.M ? StaticLayout.HYPHENATION_FREQUENCY_NORMAL : 0;
58+
59+
// Default line spacing values to match android.text.Layout constants.
60+
static final float DEFAULT_LINE_SPACING_ADD = 0.0f;
61+
static final float DEFAULT_LINE_SPACING_MULTIPLIER = 1.0f;
62+
5563
private static final String TEXT_DIR_CLASS = "android.text.TextDirectionHeuristic";
5664
private static final String TEXT_DIRS_CLASS = "android.text.TextDirectionHeuristics";
5765
private static final String TEXT_DIR_CLASS_LTR = "LTR";
@@ -70,6 +78,9 @@ final class StaticLayoutBuilderCompat {
7078

7179
private Alignment alignment;
7280
private int maxLines;
81+
private float lineSpacingAdd;
82+
private float lineSpacingMultiplier;
83+
private int hyphenationFrequency;
7384
private boolean includePad;
7485
private boolean isRtl;
7586
@Nullable private TextUtils.TruncateAt ellipsize;
@@ -82,6 +93,9 @@ private StaticLayoutBuilderCompat(CharSequence source, TextPaint paint, int widt
8293
this.end = source.length();
8394
this.alignment = Alignment.ALIGN_NORMAL;
8495
this.maxLines = Integer.MAX_VALUE;
96+
this.lineSpacingAdd = DEFAULT_LINE_SPACING_ADD;
97+
this.lineSpacingMultiplier = DEFAULT_LINE_SPACING_MULTIPLIER;
98+
this.hyphenationFrequency = DEFAULT_HYPHENATION_FREQUENCY;
8599
this.includePad = true;
86100
this.ellipsize = null;
87101
}
@@ -163,6 +177,34 @@ public StaticLayoutBuilderCompat setMaxLines(@IntRange(from = 0) int maxLines) {
163177
return this;
164178
}
165179

180+
/**
181+
* Set the line spacing addition and multiplier frequency. Only available on API level 23+.
182+
*
183+
* @param spacingAdd Line spacing addition for the resulting {@link StaticLayout}
184+
* @param lineSpacingMultiplier Line spacing multiplier for the resulting {@link StaticLayout}
185+
* @return this builder, useful for chaining
186+
* @see android.widget.TextView#setLineSpacing(float, float)
187+
*/
188+
@NonNull
189+
public StaticLayoutBuilderCompat setLineSpacing(float spacingAdd, float lineSpacingMultiplier) {
190+
this.lineSpacingAdd = spacingAdd;
191+
this.lineSpacingMultiplier = lineSpacingMultiplier;
192+
return this;
193+
}
194+
195+
/**
196+
* Set the hyphenation frequency. Only available on API level 23+.
197+
*
198+
* @param hyphenationFrequency Hyphenation frequency for the resulting {@link StaticLayout}
199+
* @return this builder, useful for chaining
200+
* @see android.widget.TextView#setHyphenationFrequency(int)
201+
*/
202+
@NonNull
203+
public StaticLayoutBuilderCompat setHyphenationFrequency(int hyphenationFrequency) {
204+
this.hyphenationFrequency = hyphenationFrequency;
205+
return this;
206+
}
207+
166208
/**
167209
* Set ellipsizing on the layout. Causes words that are longer than the view is wide, or exceeding
168210
* the number of lines (see #setMaxLines).
@@ -210,8 +252,12 @@ public StaticLayout build() throws StaticLayoutBuilderCompatException {
210252
builder.setEllipsize(ellipsize);
211253
}
212254
builder.setMaxLines(maxLines);
255+
if (lineSpacingAdd != DEFAULT_LINE_SPACING_ADD
256+
|| lineSpacingMultiplier != DEFAULT_LINE_SPACING_MULTIPLIER) {
257+
builder.setLineSpacing(lineSpacingAdd, lineSpacingMultiplier);
258+
}
213259
if (maxLines > 1) {
214-
builder.setHyphenationFrequency(StaticLayout.HYPHENATION_FREQUENCY_NORMAL);
260+
builder.setHyphenationFrequency(hyphenationFrequency);
215261
}
216262
return builder.build();
217263
}

0 commit comments

Comments
 (0)