Simplified obtainTypeface method

This commit is contained in:
Moez Bhatti
2015-11-14 18:44:50 -05:00
parent e89309ce1f
commit b80cdf9f1e
4 changed files with 24 additions and 148 deletions
@@ -20,174 +20,59 @@ public class TypefaceManager {
return typeface; return typeface;
} }
public static android.graphics.Typeface obtainTypeface(int fontFamily, int textWeight, int textStyle) throws IllegalArgumentException { public static android.graphics.Typeface obtainTypeface(int fontFamily, int textWeight) throws IllegalArgumentException {
int typeface; int typeface = Typefaces.ROBOTO_REGULAR;
if (fontFamily == FontFamily.ROBOTO) { switch (fontFamily) {
if (textWeight == TextWeight.NORMAL) { case FontFamily.ROBOTO:
switch (textStyle) { case FontFamily.LATO:
case TextStyle.NORMAL: switch (textWeight) {
case TextWeight.NORMAL:
typeface = Typefaces.ROBOTO_REGULAR; typeface = Typefaces.ROBOTO_REGULAR;
break; break;
default: case TextWeight.THIN:
throw new IllegalArgumentException("`textStyle` attribute value " + textStyle +
" is not supported for this fontFamily " + fontFamily +
" and textWeight " + textWeight);
}
} else if (textWeight == TextWeight.THIN) {
switch (textStyle) {
case TextStyle.NORMAL:
typeface = Typefaces.ROBOTO_THIN; typeface = Typefaces.ROBOTO_THIN;
break; break;
default: case TextWeight.LIGHT:
throw new IllegalArgumentException("`textStyle` attribute value " + textStyle +
" is not supported for this fontFamily " + fontFamily +
" and textWeight " + textWeight);
}
} else if (textWeight == TextWeight.LIGHT) {
switch (textStyle) {
case TextStyle.NORMAL:
typeface = Typefaces.ROBOTO_LIGHT; typeface = Typefaces.ROBOTO_LIGHT;
break; break;
default: case TextWeight.MEDIUM:
throw new IllegalArgumentException("`textStyle` attribute value " + textStyle +
" is not supported for this fontFamily " + fontFamily +
" and textWeight " + textWeight);
}
} else if (textWeight == TextWeight.MEDIUM) {
switch (textStyle) {
case TextStyle.NORMAL:
typeface = Typefaces.ROBOTO_MEDIUM; typeface = Typefaces.ROBOTO_MEDIUM;
break; break;
default:
throw new IllegalArgumentException("`textStyle` attribute value " + textStyle +
" is not supported for this fontFamily " + fontFamily +
" and textWeight " + textWeight);
} }
} else if (textWeight == TextWeight.BOLD) { break;
switch (textStyle) {
default: case FontFamily.ROBOTO_CONDENSED:
throw new IllegalArgumentException("`textStyle` attribute value " + textStyle + switch (textWeight) {
" is not supported for this fontFamily " + fontFamily + case TextWeight.LIGHT: // Condensed light is no longer supported
" and textWeight " + textWeight); case TextWeight.NORMAL:
}
} else {
throw new IllegalArgumentException("`textWeight` attribute value " + textWeight +
" is not supported for this font family " + fontFamily);
}
} else if (fontFamily == FontFamily.ROBOTO_CONDENSED) {
if (textWeight == TextWeight.NORMAL) {
switch (textStyle) {
case TextStyle.NORMAL:
typeface = Typefaces.ROBOTO_CONDENSED_REGULAR; typeface = Typefaces.ROBOTO_CONDENSED_REGULAR;
break; break;
default: case TextWeight.BOLD:
throw new IllegalArgumentException("`textStyle` attribute value " + textStyle +
" is not supported for this fontFamily " + fontFamily +
" and textWeight " + textWeight);
}
} else if (textWeight == TextWeight.LIGHT) {
switch (textStyle) {
case TextStyle.NORMAL:
typeface = Typefaces.ROBOTO_CONDENSED_LIGHT;
break;
default:
throw new IllegalArgumentException("`textStyle` attribute value " + textStyle +
" is not supported for this fontFamily " + fontFamily +
" and textWeight " + textWeight);
}
} else if (textWeight == TextWeight.BOLD) {
switch (textStyle) {
case TextStyle.NORMAL:
typeface = Typefaces.ROBOTO_CONDENSED_BOLD; typeface = Typefaces.ROBOTO_CONDENSED_BOLD;
break; break;
default:
throw new IllegalArgumentException("`textStyle` attribute value " + textStyle +
" is not supported for this fontFamily " + fontFamily +
" and textWeight " + textWeight);
} }
} else { break;
throw new IllegalArgumentException("`textWeight` attribute value " + textWeight +
" is not supported for this font family " + fontFamily);
}
} else if (fontFamily == FontFamily.LATO) {
if (textWeight == TextWeight.NORMAL) {
switch (textStyle) {
case TextStyle.NORMAL:
typeface = Typefaces.LATO_REGULAR;
break;
default:
throw new IllegalArgumentException("`textStyle` attribute value " + textStyle +
" is not supported for this fontFamily " + fontFamily +
" and textWeight " + textWeight);
}
} else if (textWeight == TextWeight.THIN) {
switch (textStyle) {
case TextStyle.NORMAL:
typeface = Typefaces.LATO_THIN;
break;
default:
throw new IllegalArgumentException("`textStyle` attribute value " + textStyle +
" is not supported for this fontFamily " + fontFamily +
" and textWeight " + textWeight);
}
} else if (textWeight == TextWeight.LIGHT) {
switch (textStyle) {
case TextStyle.NORMAL:
typeface = Typefaces.LATO_LIGHT;
break;
default:
throw new IllegalArgumentException("`textStyle` attribute value " + textStyle +
" is not supported for this fontFamily " + fontFamily +
" and textWeight " + textWeight);
}
} else if (textWeight == TextWeight.MEDIUM) {
switch (textStyle) {
case TextStyle.NORMAL:
typeface = Typefaces.LATO_MEDIUM;
break;
default:
throw new IllegalArgumentException("`textStyle` attribute value " + textStyle +
" is not supported for this fontFamily " + fontFamily +
" and textWeight " + textWeight);
}
} else if (textWeight == TextWeight.BOLD) {
switch (textStyle) {
default:
throw new IllegalArgumentException("`textStyle` attribute value " + textStyle +
" is not supported for this fontFamily " + fontFamily +
" and textWeight " + textWeight);
}
} else {
throw new IllegalArgumentException("`textWeight` attribute value " + textWeight +
" is not supported for this font family " + fontFamily);
}
} else {
throw new IllegalArgumentException("Unknown `fontFamily` attribute value " + fontFamily);
} }
return obtainTypeface(typeface); return obtainTypeface(typeface);
} }
private static Typeface createTypeface(int typefaceValue) throws IllegalArgumentException { private static Typeface createTypeface(int typefaceValue) throws IllegalArgumentException {
switch (typefaceValue) { switch (typefaceValue) {
case Typefaces.LATO_THIN:
case Typefaces.ROBOTO_THIN: case Typefaces.ROBOTO_THIN:
return Typeface.create("sans-serif-thin", Typeface.NORMAL); return Typeface.create("sans-serif-thin", Typeface.NORMAL);
case Typefaces.LATO_LIGHT:
case Typefaces.ROBOTO_LIGHT: case Typefaces.ROBOTO_LIGHT:
return Typeface.create("sans-serif-light", Typeface.NORMAL); return Typeface.create("sans-serif-light", Typeface.NORMAL);
case Typefaces.LATO_REGULAR:
case Typefaces.ROBOTO_REGULAR: case Typefaces.ROBOTO_REGULAR:
return Typeface.create("sans-serif", Typeface.NORMAL); return Typeface.create("sans-serif", Typeface.NORMAL);
case Typefaces.LATO_MEDIUM:
case Typefaces.ROBOTO_MEDIUM: case Typefaces.ROBOTO_MEDIUM:
String name = Build.VERSION.SDK_INT >= 21 ? "sans-serif-medium" : "sans-serif"; String name = Build.VERSION.SDK_INT >= 21 ? "sans-serif-medium" : "sans-serif";
int style = Build.VERSION.SDK_INT >= 21 ? Typeface.NORMAL : Typeface.BOLD; int style = Build.VERSION.SDK_INT >= 21 ? Typeface.NORMAL : Typeface.BOLD;
return Typeface.create(name, style); return Typeface.create(name, style);
case Typefaces.ROBOTO_CONDENSED_LIGHT:
case Typefaces.ROBOTO_CONDENSED_REGULAR: case Typefaces.ROBOTO_CONDENSED_REGULAR:
return Typeface.create("sans-serif-condensed", Typeface.NORMAL); return Typeface.create("sans-serif-condensed", Typeface.NORMAL);
@@ -208,13 +93,8 @@ public class TypefaceManager {
public final static int ROBOTO_LIGHT = 2; public final static int ROBOTO_LIGHT = 2;
public final static int ROBOTO_REGULAR = 4; public final static int ROBOTO_REGULAR = 4;
public final static int ROBOTO_MEDIUM = 6; public final static int ROBOTO_MEDIUM = 6;
@Deprecated public final static int ROBOTO_CONDENSED_LIGHT = 12;
public final static int ROBOTO_CONDENSED_REGULAR = 14; public final static int ROBOTO_CONDENSED_REGULAR = 14;
public final static int ROBOTO_CONDENSED_BOLD = 16; public final static int ROBOTO_CONDENSED_BOLD = 16;
@Deprecated public final static int LATO_THIN = 22;
@Deprecated public final static int LATO_LIGHT = 24;
@Deprecated public final static int LATO_REGULAR = 26;
@Deprecated public final static int LATO_MEDIUM = 28;
} }
public class FontFamily { public class FontFamily {
@@ -231,8 +111,4 @@ public class TypefaceManager {
public static final int BOLD = 4; public static final int BOLD = 4;
} }
public class TextStyle {
public static final int NORMAL = 0;
}
} }
@@ -58,7 +58,7 @@ public class AutoCompleteContactView extends RecipientEditTextView implements Li
@Override @Override
public void refresh() { public void refresh() {
setTypeface(TypefaceManager.obtainTypeface(FontManager.getFontFamily(mContext), setTypeface(TypefaceManager.obtainTypeface(FontManager.getFontFamily(mContext),
FontManager.getFontWeight(mContext, false), TypefaceManager.TextStyle.NORMAL)); FontManager.getFontWeight(mContext, false)));
setTextSize(TypedValue.COMPLEX_UNIT_SP, FontManager.getTextSize(mContext, FontManager.TEXT_TYPE_PRIMARY)); setTextSize(TypedValue.COMPLEX_UNIT_SP, FontManager.getTextSize(mContext, FontManager.TEXT_TYPE_PRIMARY));
setTextColor(ThemeManager.getTextOnBackgroundPrimary()); setTextColor(ThemeManager.getTextOnBackgroundPrimary());
@@ -97,8 +97,8 @@ public class QKEditText extends android.widget.EditText implements LiveView {
// Typeface and colors // Typeface and colors
int fontFamily = FontManager.getFontFamily(mContext); int fontFamily = FontManager.getFontFamily(mContext);
int fontWeight = FontManager.getFontWeight(mContext, false); int fontWeight = FontManager.getFontWeight(mContext, false);
setTypeface(TypefaceManager.obtainTypeface(fontFamily, fontWeight, setTypeface(TypefaceManager.obtainTypeface(fontFamily, fontWeight
TypefaceManager.TextStyle.NORMAL)); ));
setTextColor(ThemeManager.getTextOnBackgroundPrimary()); setTextColor(ThemeManager.getTextOnBackgroundPrimary());
setHintTextColor(ThemeManager.getTextOnBackgroundSecondary()); setHintTextColor(ThemeManager.getTextOnBackgroundSecondary());
@@ -162,8 +162,8 @@ public class QKTextView extends TextView implements LiveView {
// Typeface // Typeface
int fontFamily = FontManager.getFontFamily(mContext); int fontFamily = FontManager.getFontFamily(mContext);
int fontWeight = FontManager.getFontWeight(mContext, FontManager.getIsFontHeavy(mType)); int fontWeight = FontManager.getFontWeight(mContext, FontManager.getIsFontHeavy(mType));
setTypeface(TypefaceManager.obtainTypeface(fontFamily, fontWeight, setTypeface(TypefaceManager.obtainTypeface(fontFamily, fontWeight
TypefaceManager.TextStyle.NORMAL)); ));
// Text size and color // Text size and color
setTextSize(TypedValue.COMPLEX_UNIT_SP, FontManager.getTextSize(mContext, mType)); setTextSize(TypedValue.COMPLEX_UNIT_SP, FontManager.getTextSize(mContext, mType));