diff options
| author | Raph Levien <raph@google.com> | 2012-05-11 09:31:22 -0700 |
|---|---|---|
| committer | Android (Google) Code Review <android-gerrit@google.com> | 2012-05-11 09:31:22 -0700 |
| commit | 75ef98f8210fa1845d04a6e8206f4c7be5499052 (patch) | |
| tree | 52fc4bca694157242d49d90aa639dfa05ffede12 /core/java | |
| parent | 156c79289dec581536c0ee23ffaa355909827587 (diff) | |
| parent | d570e8987ae50ee101c3d4cfa973e1a6b6ecc8f5 (diff) | |
Merge "Add fontFamily XML attribute to select font family by string" into jb-dev
Diffstat (limited to 'core/java')
| -rw-r--r-- | core/java/android/text/style/TextAppearanceSpan.java | 41 | ||||
| -rw-r--r-- | core/java/android/widget/TextView.java | 34 |
2 files changed, 51 insertions, 24 deletions
diff --git a/core/java/android/text/style/TextAppearanceSpan.java b/core/java/android/text/style/TextAppearanceSpan.java index 5fd7c57db22e..ecbf4bc0522b 100644 --- a/core/java/android/text/style/TextAppearanceSpan.java +++ b/core/java/android/text/style/TextAppearanceSpan.java @@ -68,24 +68,29 @@ public class TextAppearanceSpan extends MetricAffectingSpan implements Parcelabl TextAppearance_textSize, -1); mStyle = a.getInt(com.android.internal.R.styleable.TextAppearance_textStyle, 0); - int tf = a.getInt(com.android.internal.R.styleable.TextAppearance_typeface, 0); - - switch (tf) { - case 1: - mTypeface = "sans"; - break; - - case 2: - mTypeface = "serif"; - break; - - case 3: - mTypeface = "monospace"; - break; - - default: - mTypeface = null; - break; + String family = a.getString(com.android.internal.R.styleable.TextAppearance_fontFamily); + if (family != null) { + mTypeface = family; + } else { + int tf = a.getInt(com.android.internal.R.styleable.TextAppearance_typeface, 0); + + switch (tf) { + case 1: + mTypeface = "sans"; + break; + + case 2: + mTypeface = "serif"; + break; + + case 3: + mTypeface = "monospace"; + break; + + default: + mTypeface = null; + break; + } } a.recycle(); diff --git a/core/java/android/widget/TextView.java b/core/java/android/widget/TextView.java index 555c974360fc..098a034b2e64 100644 --- a/core/java/android/widget/TextView.java +++ b/core/java/android/widget/TextView.java @@ -154,6 +154,7 @@ import java.util.Locale; * @attr ref android.R.styleable#TextView_textColorLink * @attr ref android.R.styleable#TextView_textSize * @attr ref android.R.styleable#TextView_textScaleX + * @attr ref android.R.styleable#TextView_fontFamily * @attr ref android.R.styleable#TextView_typeface * @attr ref android.R.styleable#TextView_textStyle * @attr ref android.R.styleable#TextView_cursorVisible @@ -464,6 +465,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener ColorStateList textColorHint = null; ColorStateList textColorLink = null; int textSize = 15; + String fontFamily = null; int typefaceIndex = -1; int styleIndex = -1; boolean allCaps = false; @@ -516,6 +518,10 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener typefaceIndex = appearance.getInt(attr, -1); break; + case com.android.internal.R.styleable.TextAppearance_fontFamily: + fontFamily = appearance.getString(attr); + break; + case com.android.internal.R.styleable.TextAppearance_textStyle: styleIndex = appearance.getInt(attr, -1); break; @@ -781,6 +787,10 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener styleIndex = a.getInt(attr, styleIndex); break; + case com.android.internal.R.styleable.TextView_fontFamily: + fontFamily = a.getString(attr); + break; + case com.android.internal.R.styleable.TextView_password: password = a.getBoolean(attr, password); break; @@ -1051,7 +1061,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener typefaceIndex = MONOSPACE; } - setTypefaceByIndex(typefaceIndex, styleIndex); + setTypefaceFromAttrs(fontFamily, typefaceIndex, styleIndex); if (shadowcolor != 0) { setShadowLayer(r, dx, dy, shadowcolor); @@ -1111,8 +1121,15 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener } } - private void setTypefaceByIndex(int typefaceIndex, int styleIndex) { + private void setTypefaceFromAttrs(String familyName, int typefaceIndex, int styleIndex) { Typeface tf = null; + if (familyName != null) { + tf = Typeface.create(familyName, styleIndex); + if (tf != null) { + setTypeface(tf); + return; + } + } switch (typefaceIndex) { case SANS: tf = Typeface.SANS_SERIF; @@ -2160,14 +2177,17 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener setLinkTextColor(colors); } + String familyName; int typefaceIndex, styleIndex; + familyName = appearance.getString(com.android.internal.R.styleable. + TextAppearance_fontFamily); typefaceIndex = appearance.getInt(com.android.internal.R.styleable. TextAppearance_typeface, -1); styleIndex = appearance.getInt(com.android.internal.R.styleable. TextAppearance_textStyle, -1); - setTypefaceByIndex(typefaceIndex, styleIndex); + setTypefaceFromAttrs(familyName, typefaceIndex, styleIndex); if (appearance.getBoolean(com.android.internal.R.styleable.TextAppearance_textAllCaps, false)) { @@ -2269,6 +2289,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener * * @see #getTypeface() * + * @attr ref android.R.styleable#TextView_fontFamily * @attr ref android.R.styleable#TextView_typeface * @attr ref android.R.styleable#TextView_textStyle */ @@ -2290,6 +2311,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener * * @see #setTypeface(Typeface) * + * @attr ref android.R.styleable#TextView_fontFamily * @attr ref android.R.styleable#TextView_typeface * @attr ref android.R.styleable#TextView_textStyle */ @@ -3690,15 +3712,15 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener boolean forceUpdate = false; if (isPassword) { setTransformationMethod(PasswordTransformationMethod.getInstance()); - setTypefaceByIndex(MONOSPACE, 0); + setTypefaceFromAttrs(null /* fontFamily */, MONOSPACE, 0); } else if (isVisiblePassword) { if (mTransformation == PasswordTransformationMethod.getInstance()) { forceUpdate = true; } - setTypefaceByIndex(MONOSPACE, 0); + setTypefaceFromAttrs(null /* fontFamily */, MONOSPACE, 0); } else if (wasPassword || wasVisiblePassword) { // not in password mode, clean up typeface and transformation - setTypefaceByIndex(-1, -1); + setTypefaceFromAttrs(null /* fontFamily */, -1, -1); if (mTransformation == PasswordTransformationMethod.getInstance()) { forceUpdate = true; } |
