CustomFontUtils.java
3.8 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package com.xdy.ui.fontview;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.widget.TextView;
import com.xdy.ui.R;
import java.util.HashMap;
import java.util.Map;
/**
* Utility class to handle font changes within a TextView.
*/
public class CustomFontUtils {
private static final String FONTS_DIR = "fonts";
private static final String FONT_BOLD_NAME = "bold";
private static final String FONT_ITALIC_NAME = "italic";
private static String fonts[] = {};
private static final Map<String, Typeface> sTypefaceMap = new HashMap<>();
public static Typeface getTypeFace(final TextView v,
final AttributeSet attrs) {
final TypedArray tArray = v.getContext().getTheme()
.obtainStyledAttributes(attrs, R.styleable.ui_customFont, 0, 0);
String fontName = "";
try {
fontName = tArray.getString(R.styleable.ui_customFont_ui_font);
} finally {
tArray.recycle();
}
if (!TextUtils.isEmpty(fontName)) {
final int style;
final Typeface typeface = v.getTypeface();
if (typeface == null) {
style = Typeface.NORMAL;
} else {
style = typeface.getStyle();
}
String mFontName = getFontFile(v.getContext(), fontName, style);
if (sTypefaceMap.containsKey(mFontName)) {
return sTypefaceMap.get(mFontName);
}
Typeface newTypeface = typeface;
if (!TextUtils.isEmpty(mFontName)) {
try {
newTypeface = Typeface.createFromAsset(v.getContext()
.getAssets(), FONTS_DIR + "/" + mFontName);
} catch (Exception e) {
newTypeface = typeface;
}
}
sTypefaceMap.put(mFontName, newTypeface);
return newTypeface;
}
return null;
}
private static String getFontFile(final Context context,
final String fontName, final int style) {
if (TextUtils.isEmpty(fontName)) {
return "";
}
String fonts[] = getFontsFile(context);
for (String currFile : fonts) {
String currName = currFile.toLowerCase();
if (currName.contains(fontName.toLowerCase())) {
if (style == Typeface.BOLD_ITALIC
&& currName.contains(FONT_BOLD_NAME)
&& currName.contains(FONT_ITALIC_NAME)) {
return currFile;
} else if (style == Typeface.BOLD
&& currName.contains(FONT_BOLD_NAME)) {
return currFile;
} else if (style == Typeface.ITALIC
&& currName.contains(FONT_ITALIC_NAME)) {
return currFile;
}
}
}
// if there's no match for any style return the "regular" font
for (String currFile : fonts) {
int lastIndex = currFile.lastIndexOf(".");
if (lastIndex > 0) {
String fileName = currFile.substring(0, lastIndex);
if (fileName.equalsIgnoreCase(fontName)) {
return currFile;
}
}
}
return "";
}
private static String[] getFontsFile(Context context) {
if (fonts == null || fonts.length == 0) {
try {
fonts = context.getAssets().list(FONTS_DIR);
if (fonts == null || fonts.length == 0) {
}
} catch (Exception e) {
}
}
return fonts;
}
}