/
CustomFontUtils.java
73 lines (58 loc) · 2.89 KB
/
CustomFontUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package io.futurestud.tutorials.customfont;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;
/**
* Created by norman on 3/8/15.
*/
public class CustomFontUtils {
public static final String ANDROID_SCHEMA = "http://schemas.android.com/apk/res/android";
public static void applyCustomFont(TextView customFontTextView, Context context, AttributeSet attrs) {
TypedArray attributeArray = context.obtainStyledAttributes(
attrs,
R.styleable.CustomFontTextView);
String fontName = attributeArray.getString(R.styleable.CustomFontTextView_font);
// check if a special textStyle was used (e.g. extra bold)
int textStyle = attributeArray.getInt(R.styleable.CustomFontTextView_textStyle, 0);
// if nothing extra was used, fall back to regular android:textStyle parameter
if (textStyle == 0) {
textStyle = attrs.getAttributeIntValue(ANDROID_SCHEMA, "textStyle", Typeface.NORMAL);
}
Typeface customFont = selectTypeface(context, fontName, textStyle);
customFontTextView.setTypeface(customFont);
attributeArray.recycle();
}
private static Typeface selectTypeface(Context context, String fontName, int textStyle) {
if (fontName.contentEquals(context.getString(R.string.font_name_fontawesome))) {
return FontCache.getTypeface("fontawesome.ttf", context);
}
else if (fontName.contentEquals(context.getString(R.string.font_name_source_sans_pro))) {
/*
information about the TextView textStyle:
http://developer.android.com/reference/android/R.styleable.html#TextView_textStyle
*/
switch (textStyle) {
case Typeface.BOLD: // bold
return FontCache.getTypeface("SourceSansPro-Bold.ttf", context);
case Typeface.ITALIC: // italic
return FontCache.getTypeface("SourceSansPro-Italic.ttf", context);
case Typeface.BOLD_ITALIC: // bold italic
return FontCache.getTypeface("SourceSansPro-BoldItalic.ttf", context);
case 10: // extra light, equals @integer/font_style_extra_light
return FontCache.getTypeface("SourceSansPro-ExtraLight.ttf", context);
case 11: // extra bold, equals @integer/font_style_extra_bold
return FontCache.getTypeface("SourceSansPro-Black.ttf", context);
case Typeface.NORMAL: // regular
default:
return FontCache.getTypeface("SourceSansPro-Regular.ttf", context);
}
}
else {
// no matching font found
// return null so Android just uses the standard font (Roboto)
return null;
}
}
}