Skip to content

Commit a9debc7

Browse files
hunterstichpaulfthomas
authored andcommitted
[Typography] Add variable font support to TextAppearance
PiperOrigin-RevId: 650200668
1 parent 0f3975d commit a9debc7

File tree

4 files changed

+48
-8
lines changed

4 files changed

+48
-8
lines changed

catalog/java/io/material/catalog/font/FontMainDemoFragment.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
import android.content.Context;
2222
import android.content.res.TypedArray;
23+
import android.os.Build.VERSION;
24+
import android.os.Build.VERSION_CODES;
2325
import android.os.Bundle;
2426
import androidx.recyclerview.widget.DividerItemDecoration;
2527
import androidx.recyclerview.widget.LinearLayoutManager;
@@ -103,6 +105,16 @@ protected String convertFontFamilyToDescription(String fontFamily) {
103105
}
104106
}
105107

108+
@NonNull
109+
protected String maybeGetFontVariationSettingsDescription(
110+
@Nullable String fontVariationSettings) {
111+
if (VERSION.SDK_INT < VERSION_CODES.O || fontVariationSettings == null) {
112+
return "";
113+
} else {
114+
return " " + fontVariationSettings;
115+
}
116+
}
117+
106118
private class FontStyleAdapter extends Adapter<FontStyleViewHolder> {
107119

108120
private final List<Integer> styles = new ArrayList<>();
@@ -191,7 +203,8 @@ private String createDescription(String name, @StyleRes int style) {
191203
+ convertFontFamilyToDescription(textAppearance.fontFamily)
192204
+ " "
193205
+ pxToSp(textAppearance.getTextSize())
194-
+ "sp";
206+
+ "sp"
207+
+ maybeGetFontVariationSettingsDescription(textAppearance.fontVariationSettings);
195208
}
196209

197210
private int pxToSp(float px) {

catalog/java/io/material/catalog/font/res/layout/cat_font_styles_item.xml

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,19 @@
1515
limitations under the License.
1616
-->
1717

18-
<FrameLayout
18+
<LinearLayout
1919
xmlns:android="http://schemas.android.com/apk/res/android"
2020
xmlns:app="http://schemas.android.com/apk/res-auto"
2121
android:layout_width="match_parent"
22-
android:layout_height="wrap_content">
22+
android:layout_height="wrap_content"
23+
android:orientation="horizontal">
2324

2425
<LinearLayout
25-
android:layout_width="match_parent"
26+
android:layout_width="0dp"
2627
android:layout_height="wrap_content"
2728
android:paddingTop="8dp"
2829
android:paddingBottom="8dp"
30+
android:layout_weight="1"
2931
android:orientation="vertical">
3032
<TextView
3133
android:id="@+id/description"
@@ -36,8 +38,7 @@
3638
<TextView
3739
android:id="@+id/name"
3840
android:layout_width="wrap_content"
39-
android:layout_height="wrap_content"
40-
android:maxLines="1"/>
41+
android:layout_height="wrap_content" />
4142
</LinearLayout>
4243

4344
<ImageView
@@ -49,5 +50,5 @@
4950
android:background="?attr/selectableItemBackgroundBorderless"
5051
android:contentDescription="Information"
5152
app:srcCompat="@drawable/ic_info_outline_24px"
52-
app:tint="?attr/colorControlNormal"/>
53-
</FrameLayout>
53+
app:tint="?attr/colorControlNormal" />
54+
</LinearLayout>

lib/java/com/google/android/material/resources/TextAppearance.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import androidx.annotation.FontRes;
3535
import androidx.annotation.NonNull;
3636
import androidx.annotation.Nullable;
37+
import androidx.annotation.RequiresApi;
3738
import androidx.annotation.RestrictTo;
3839
import androidx.annotation.RestrictTo.Scope;
3940
import androidx.annotation.StyleRes;
@@ -62,6 +63,7 @@ public class TextAppearance {
6263
@Nullable public final ColorStateList textColorLink;
6364
@Nullable public final ColorStateList shadowColor;
6465
@Nullable public final String fontFamily;
66+
@Nullable public String fontVariationSettings;
6567

6668
public final int textStyle;
6769
public final int typeface;
@@ -119,6 +121,13 @@ public TextAppearance(@NonNull Context context, @StyleRes int id) {
119121
a = context.obtainStyledAttributes(id, R.styleable.MaterialTextAppearance);
120122
hasLetterSpacing = a.hasValue(R.styleable.MaterialTextAppearance_android_letterSpacing);
121123
letterSpacing = a.getFloat(R.styleable.MaterialTextAppearance_android_letterSpacing, 0);
124+
if (VERSION.SDK_INT >= VERSION_CODES.O) {
125+
int fontVariationSettingsIndex = MaterialResources.getIndexWithValue(
126+
a,
127+
R.styleable.MaterialTextAppearance_fontVariationSettings,
128+
R.styleable.MaterialTextAppearance_android_fontVariationSettings);
129+
fontVariationSettings = a.getString(fontVariationSettingsIndex);
130+
}
122131
a.recycle();
123132
} else {
124133
hasLetterSpacing = false;
@@ -354,6 +363,10 @@ public void updateTextPaintMeasureState(
354363

355364
textPaint.setTextSize(textSize);
356365

366+
if (VERSION.SDK_INT >= VERSION_CODES.O) {
367+
textPaint.setFontVariationSettings(fontVariationSettings);
368+
}
369+
357370
if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
358371
if (hasLetterSpacing) {
359372
textPaint.setLetterSpacing(letterSpacing);
@@ -378,6 +391,17 @@ public void setTextSize(float textSize) {
378391
this.textSize = textSize;
379392
}
380393

394+
@RequiresApi(VERSION_CODES.O)
395+
@Nullable
396+
public String getFontVariationSettings() {
397+
return fontVariationSettings;
398+
}
399+
400+
@RequiresApi(VERSION_CODES.O)
401+
public void setFontVariationSettings(@Nullable String fontVariationSettings) {
402+
this.fontVariationSettings = fontVariationSettings;
403+
}
404+
381405
private boolean maybeLoadFontSynchronously(Context context) {
382406
if (TextAppearanceConfig.shouldLoadFontSynchronously()) {
383407
getFont(context);

lib/java/com/google/android/material/resources/res/values/attrs.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
<attr name="android:lineHeight"/>
4444
<attr name="lineHeight" format="dimension"/>
4545
<attr name="android:letterSpacing"/>
46+
<attr name="android:fontVariationSettings"/>
47+
<attr name="fontVariationSettings" format="string"/>
4648
</declare-styleable>
4749

4850
</resources>

0 commit comments

Comments
 (0)