Skip to content

Commit

Permalink
new function to find a font inside assets
Browse files Browse the repository at this point in the history
  • Loading branch information
medyo committed Mar 12, 2016
1 parent bcb85ed commit fa80eb3
Showing 1 changed file with 21 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package mehdi.sakout.fancybuttons;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Typeface;

import java.util.Arrays;

public class Utils {

Expand All @@ -12,4 +16,21 @@ 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){
try{
AssetManager assets = context.getResources().getAssets();
if (Arrays.asList(assets.list("")).contains(fontName)){
return Typeface.createFromAsset(context.getAssets(), String.format("%s",fontName));
}else if (Arrays.asList(assets.list("fonts")).contains(fontName)){
return Typeface.createFromAsset(context.getAssets(), String.format("fonts/%s",fontName));
}else if (Arrays.asList(assets.list("iconfonts")).contains(fontName)){
return Typeface.createFromAsset(context.getAssets(), String.format("iconfonts/%s",fontName));
}else{
return Typeface.createFromAsset(context.getAssets(), String.format("%s", defaultFont));
}
}catch (Exception e){
return Typeface.createFromAsset(context.getAssets(), String.format("%s", defaultFont));
}
}

}

0 comments on commit fa80eb3

Please sign in to comment.