Skip to content

Commit

Permalink
fix bug custom fonts bug
Browse files Browse the repository at this point in the history
  • Loading branch information
medyo committed May 8, 2016
1 parent eea521c commit 7767352
Showing 1 changed file with 19 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import android.content.res.AssetManager;
import android.graphics.Typeface;
import android.text.TextUtils;

import java.io.File;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
Expand All @@ -21,14 +21,22 @@ public static int spToPx(final Context context, final float sp) {
return Math.round(sp * context.getResources().getDisplayMetrics().scaledDensity);
}

public static Typeface findFont(Context context, String fontName, String defaultFont){
public static Typeface findFont(Context context, String fonPath, String defaultFontPath){

String fontName = new File(fonPath).getName();
String defaultFontName = "";
if (!TextUtils.isEmpty(defaultFontPath)){
defaultFontName = new File(defaultFontPath).getName();
}

if (cachedFontMap.containsKey(fontName)){
return cachedFontMap.get(fontName);
}else{
try{
AssetManager assets = context.getResources().getAssets();
if (Arrays.asList(assets.list("")).contains(fontName)){
Typeface typeface = Typeface.createFromAsset(context.getAssets(), String.format("%s",fontName));

if (Arrays.asList(assets.list("")).contains(fonPath)){
Typeface typeface = Typeface.createFromAsset(context.getAssets(), fontName);
cachedFontMap.put(fontName, typeface);
return typeface;
}else if (Arrays.asList(assets.list("fonts")).contains(fontName)){
Expand All @@ -39,18 +47,16 @@ public static Typeface findFont(Context context, String fontName, String default
Typeface typeface = Typeface.createFromAsset(context.getAssets(), String.format("iconfonts/%s",fontName));
cachedFontMap.put(fontName, typeface);
return typeface;
}else{
}else if (!TextUtils.isEmpty(defaultFontPath) && Arrays.asList(assets.list("")).contains(defaultFontPath)){
Typeface typeface = Typeface.createFromAsset(context.getAssets(), defaultFontPath);
cachedFontMap.put(defaultFontName, typeface);
return typeface;
} else {
throw new Exception("Font not Found");

}

}catch (Exception e){
if (!TextUtils.isEmpty(defaultFont)){
Typeface typeface = Typeface.createFromAsset(context.getAssets(), String.format("%s", defaultFont));
cachedFontMap.put(fontName, typeface);
return typeface;
}else{
return Typeface.DEFAULT;
}
return Typeface.DEFAULT;
}
}

Expand Down

0 comments on commit 7767352

Please sign in to comment.