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;
}
public static android.graphics.Typeface obtainTypeface(int fontFamily, int textWeight, int textStyle) throws IllegalArgumentException {
int typeface;
if (fontFamily == FontFamily.ROBOTO) {
if (textWeight == TextWeight.NORMAL) {
switch (textStyle) {
case TextStyle.NORMAL:
public static android.graphics.Typeface obtainTypeface(int fontFamily, int textWeight) throws IllegalArgumentException {
int typeface = Typefaces.ROBOTO_REGULAR;
switch (fontFamily) {
case FontFamily.ROBOTO:
case FontFamily.LATO:
switch (textWeight) {
case TextWeight.NORMAL:
typeface = Typefaces.ROBOTO_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:
case TextWeight.THIN:
typeface = Typefaces.ROBOTO_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:
case TextWeight.LIGHT:
typeface = Typefaces.ROBOTO_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:
case TextWeight.MEDIUM:
typeface = Typefaces.ROBOTO_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 if (fontFamily == FontFamily.ROBOTO_CONDENSED) {
if (textWeight == TextWeight.NORMAL) {
switch (textStyle) {
case TextStyle.NORMAL:
break;
case FontFamily.ROBOTO_CONDENSED:
switch (textWeight) {
case TextWeight.LIGHT: // Condensed light is no longer supported
case TextWeight.NORMAL:
typeface = Typefaces.ROBOTO_CONDENSED_REGULAR;
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.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:
case TextWeight.BOLD:
typeface = Typefaces.ROBOTO_CONDENSED_BOLD;
break;
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 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);
break;
}
return obtainTypeface(typeface);
}
private static Typeface createTypeface(int typefaceValue) throws IllegalArgumentException {
switch (typefaceValue) {
case Typefaces.LATO_THIN:
case Typefaces.ROBOTO_THIN:
return Typeface.create("sans-serif-thin", Typeface.NORMAL);
case Typefaces.LATO_LIGHT:
case Typefaces.ROBOTO_LIGHT:
return Typeface.create("sans-serif-light", Typeface.NORMAL);
case Typefaces.LATO_REGULAR:
case Typefaces.ROBOTO_REGULAR:
return Typeface.create("sans-serif", Typeface.NORMAL);
case Typefaces.LATO_MEDIUM:
case Typefaces.ROBOTO_MEDIUM:
String name = Build.VERSION.SDK_INT >= 21 ? "sans-serif-medium" : "sans-serif";
int style = Build.VERSION.SDK_INT >= 21 ? Typeface.NORMAL : Typeface.BOLD;
return Typeface.create(name, style);
case Typefaces.ROBOTO_CONDENSED_LIGHT:
case Typefaces.ROBOTO_CONDENSED_REGULAR:
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_REGULAR = 4;
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_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 {
@@ -231,8 +111,4 @@ public class TypefaceManager {
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
public void refresh() {
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));
setTextColor(ThemeManager.getTextOnBackgroundPrimary());
@@ -97,8 +97,8 @@ public class QKEditText extends android.widget.EditText implements LiveView {
// Typeface and colors
int fontFamily = FontManager.getFontFamily(mContext);
int fontWeight = FontManager.getFontWeight(mContext, false);
setTypeface(TypefaceManager.obtainTypeface(fontFamily, fontWeight,
TypefaceManager.TextStyle.NORMAL));
setTypeface(TypefaceManager.obtainTypeface(fontFamily, fontWeight
));
setTextColor(ThemeManager.getTextOnBackgroundPrimary());
setHintTextColor(ThemeManager.getTextOnBackgroundSecondary());
@@ -162,8 +162,8 @@ public class QKTextView extends TextView implements LiveView {
// Typeface
int fontFamily = FontManager.getFontFamily(mContext);
int fontWeight = FontManager.getFontWeight(mContext, FontManager.getIsFontHeavy(mType));
setTypeface(TypefaceManager.obtainTypeface(fontFamily, fontWeight,
TypefaceManager.TextStyle.NORMAL));
setTypeface(TypefaceManager.obtainTypeface(fontFamily, fontWeight
));
// Text size and color
setTextSize(TypedValue.COMPLEX_UNIT_SP, FontManager.getTextSize(mContext, mType));