Skip to content

Commit 75ef98f

Browse files
raphlinusAndroid (Google) Code Review
authored andcommitted
Merge "Add fontFamily XML attribute to select font family by string" into jb-dev
2 parents 156c792 + d570e89 commit 75ef98f

File tree

5 files changed

+61
-24
lines changed

5 files changed

+61
-24
lines changed

api/current.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,7 @@ package android {
485485
field public static final int focusable = 16842970; // 0x10100da
486486
field public static final int focusableInTouchMode = 16842971; // 0x10100db
487487
field public static final int focusedMonthDateColor = 16843587; // 0x1010343
488+
field public static final int fontFamily = 16843692; // 0x10103ac
488489
field public static final int footerDividersEnabled = 16843311; // 0x101022f
489490
field public static final int foreground = 16843017; // 0x1010109
490491
field public static final int foregroundGravity = 16843264; // 0x1010200

core/java/android/text/style/TextAppearanceSpan.java

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -68,24 +68,29 @@ public TextAppearanceSpan(Context context, int appearance, int colorList) {
6868
TextAppearance_textSize, -1);
6969

7070
mStyle = a.getInt(com.android.internal.R.styleable.TextAppearance_textStyle, 0);
71-
int tf = a.getInt(com.android.internal.R.styleable.TextAppearance_typeface, 0);
72-
73-
switch (tf) {
74-
case 1:
75-
mTypeface = "sans";
76-
break;
77-
78-
case 2:
79-
mTypeface = "serif";
80-
break;
81-
82-
case 3:
83-
mTypeface = "monospace";
84-
break;
85-
86-
default:
87-
mTypeface = null;
88-
break;
71+
String family = a.getString(com.android.internal.R.styleable.TextAppearance_fontFamily);
72+
if (family != null) {
73+
mTypeface = family;
74+
} else {
75+
int tf = a.getInt(com.android.internal.R.styleable.TextAppearance_typeface, 0);
76+
77+
switch (tf) {
78+
case 1:
79+
mTypeface = "sans";
80+
break;
81+
82+
case 2:
83+
mTypeface = "serif";
84+
break;
85+
86+
case 3:
87+
mTypeface = "monospace";
88+
break;
89+
90+
default:
91+
mTypeface = null;
92+
break;
93+
}
8994
}
9095

9196
a.recycle();

core/java/android/widget/TextView.java

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@
154154
* @attr ref android.R.styleable#TextView_textColorLink
155155
* @attr ref android.R.styleable#TextView_textSize
156156
* @attr ref android.R.styleable#TextView_textScaleX
157+
* @attr ref android.R.styleable#TextView_fontFamily
157158
* @attr ref android.R.styleable#TextView_typeface
158159
* @attr ref android.R.styleable#TextView_textStyle
159160
* @attr ref android.R.styleable#TextView_cursorVisible
@@ -464,6 +465,7 @@ public TextView(Context context, AttributeSet attrs, int defStyle) {
464465
ColorStateList textColorHint = null;
465466
ColorStateList textColorLink = null;
466467
int textSize = 15;
468+
String fontFamily = null;
467469
int typefaceIndex = -1;
468470
int styleIndex = -1;
469471
boolean allCaps = false;
@@ -516,6 +518,10 @@ public TextView(Context context, AttributeSet attrs, int defStyle) {
516518
typefaceIndex = appearance.getInt(attr, -1);
517519
break;
518520

521+
case com.android.internal.R.styleable.TextAppearance_fontFamily:
522+
fontFamily = appearance.getString(attr);
523+
break;
524+
519525
case com.android.internal.R.styleable.TextAppearance_textStyle:
520526
styleIndex = appearance.getInt(attr, -1);
521527
break;
@@ -781,6 +787,10 @@ public TextView(Context context, AttributeSet attrs, int defStyle) {
781787
styleIndex = a.getInt(attr, styleIndex);
782788
break;
783789

790+
case com.android.internal.R.styleable.TextView_fontFamily:
791+
fontFamily = a.getString(attr);
792+
break;
793+
784794
case com.android.internal.R.styleable.TextView_password:
785795
password = a.getBoolean(attr, password);
786796
break;
@@ -1051,7 +1061,7 @@ public TextView(Context context, AttributeSet attrs, int defStyle) {
10511061
typefaceIndex = MONOSPACE;
10521062
}
10531063

1054-
setTypefaceByIndex(typefaceIndex, styleIndex);
1064+
setTypefaceFromAttrs(fontFamily, typefaceIndex, styleIndex);
10551065

10561066
if (shadowcolor != 0) {
10571067
setShadowLayer(r, dx, dy, shadowcolor);
@@ -1111,8 +1121,15 @@ public TextView(Context context, AttributeSet attrs, int defStyle) {
11111121
}
11121122
}
11131123

1114-
private void setTypefaceByIndex(int typefaceIndex, int styleIndex) {
1124+
private void setTypefaceFromAttrs(String familyName, int typefaceIndex, int styleIndex) {
11151125
Typeface tf = null;
1126+
if (familyName != null) {
1127+
tf = Typeface.create(familyName, styleIndex);
1128+
if (tf != null) {
1129+
setTypeface(tf);
1130+
return;
1131+
}
1132+
}
11161133
switch (typefaceIndex) {
11171134
case SANS:
11181135
tf = Typeface.SANS_SERIF;
@@ -2160,14 +2177,17 @@ public void setTextAppearance(Context context, int resid) {
21602177
setLinkTextColor(colors);
21612178
}
21622179

2180+
String familyName;
21632181
int typefaceIndex, styleIndex;
21642182

2183+
familyName = appearance.getString(com.android.internal.R.styleable.
2184+
TextAppearance_fontFamily);
21652185
typefaceIndex = appearance.getInt(com.android.internal.R.styleable.
21662186
TextAppearance_typeface, -1);
21672187
styleIndex = appearance.getInt(com.android.internal.R.styleable.
21682188
TextAppearance_textStyle, -1);
21692189

2170-
setTypefaceByIndex(typefaceIndex, styleIndex);
2190+
setTypefaceFromAttrs(familyName, typefaceIndex, styleIndex);
21712191

21722192
if (appearance.getBoolean(com.android.internal.R.styleable.TextAppearance_textAllCaps,
21732193
false)) {
@@ -2269,6 +2289,7 @@ public void setTextScaleX(float size) {
22692289
*
22702290
* @see #getTypeface()
22712291
*
2292+
* @attr ref android.R.styleable#TextView_fontFamily
22722293
* @attr ref android.R.styleable#TextView_typeface
22732294
* @attr ref android.R.styleable#TextView_textStyle
22742295
*/
@@ -2290,6 +2311,7 @@ public void setTypeface(Typeface tf) {
22902311
*
22912312
* @see #setTypeface(Typeface)
22922313
*
2314+
* @attr ref android.R.styleable#TextView_fontFamily
22932315
* @attr ref android.R.styleable#TextView_typeface
22942316
* @attr ref android.R.styleable#TextView_textStyle
22952317
*/
@@ -3690,15 +3712,15 @@ public void setInputType(int type) {
36903712
boolean forceUpdate = false;
36913713
if (isPassword) {
36923714
setTransformationMethod(PasswordTransformationMethod.getInstance());
3693-
setTypefaceByIndex(MONOSPACE, 0);
3715+
setTypefaceFromAttrs(null /* fontFamily */, MONOSPACE, 0);
36943716
} else if (isVisiblePassword) {
36953717
if (mTransformation == PasswordTransformationMethod.getInstance()) {
36963718
forceUpdate = true;
36973719
}
3698-
setTypefaceByIndex(MONOSPACE, 0);
3720+
setTypefaceFromAttrs(null /* fontFamily */, MONOSPACE, 0);
36993721
} else if (wasPassword || wasVisiblePassword) {
37003722
// not in password mode, clean up typeface and transformation
3701-
setTypefaceByIndex(-1, -1);
3723+
setTypefaceFromAttrs(null /* fontFamily */, -1, -1);
37023724
if (mTransformation == PasswordTransformationMethod.getInstance()) {
37033725
forceUpdate = true;
37043726
}

core/res/res/values/attrs.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -857,6 +857,9 @@
857857
-->
858858
<attr name="textSize" format="dimension" />
859859

860+
<!-- Default font family. -->
861+
<attr name="fontFamily" format="string" />
862+
860863
<!-- Default text typeface. -->
861864
<attr name="typeface">
862865
<enum name="normal" value="0" />
@@ -2999,6 +3002,8 @@
29993002
<attr name="textStyle" />
30003003
<!-- Typeface (normal, sans, serif, monospace) for the text. -->
30013004
<attr name="typeface" />
3005+
<!-- Font family (named by string) for the text. -->
3006+
<attr name="fontFamily" />
30023007
<!-- Color of the text selection highlight. -->
30033008
<attr name="textColorHighlight" />
30043009
<!-- Color of the hint text. -->
@@ -3044,6 +3049,8 @@
30443049
<attr name="typeface" />
30453050
<!-- Style (bold, italic, bolditalic) for the text. -->
30463051
<attr name="textStyle" />
3052+
<!-- Font family (named by string) for the text. -->
3053+
<attr name="fontFamily" />
30473054
<!-- Text color for links. -->
30483055
<attr name="textColorLink" />
30493056
<!-- Makes the cursor visible (the default) or invisible. -->

core/res/res/values/public.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3605,4 +3605,6 @@
36053605
<public type="attr" name="importantForAccessibility" id="0x010103aa" />
36063606
<public type="attr" name="kcm" id="0x010103ab" />
36073607

3608+
<public type="attr" name="fontFamily" />
3609+
36083610
</resources>

0 commit comments

Comments
 (0)