Created
March 25, 2016 08:03
-
-
Save sw1ftc0d3r/df4f375311e5d4d9591a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @author Andrea Cappelli | |
*/ | |
public class CustomTextView extends TextView { | |
private static final String TAG = CustomTextView.class.getName(); | |
public CustomTextView(Context context) { | |
super(context); | |
} | |
public CustomTextView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
setCustomFont(context, attrs); | |
} | |
public CustomTextView(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
setCustomFont(context, attrs); | |
} | |
private void setCustomFont(Context ctx, AttributeSet attrs) { | |
TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.CustomTextView); | |
String customFont = a.getString(R.styleable.CustomTextView_customFont); | |
setCustomFont(ctx, customFont); | |
a.recycle(); | |
} | |
public boolean setCustomFont(Context ctx, String asset) { | |
Typeface typeface = null; | |
try { | |
typeface = Typeface.createFromAsset(ctx.getAssets(), asset); | |
} catch (Exception e) { | |
LogHelper.e(TAG, "Unable to load typeface: " + e.getMessage()); | |
return false; | |
} | |
setTypeface(typeface); | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment