Last active
November 3, 2016 08:52
-
-
Save voghDev/6b83aa0fd72fafc361aae1928d6abc98 to your computer and use it in GitHub Desktop.
TextView subclass that applies a customized font. You also need FontFactory class, available in the first comment of this gist
This file contains 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
import android.content.Context; | |
import android.graphics.Typeface; | |
import android.util.AttributeSet; | |
import android.widget.TextView; | |
public class StyledTextView extends TextView { | |
private static final int NO_STYLE = -1; | |
public StyledTextView(Context context) { | |
super(context); | |
style(null, NO_STYLE); | |
} | |
public StyledTextView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
style(attrs, NO_STYLE); | |
} | |
public StyledTextView(Context context, AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
style(attrs, defStyleAttr); | |
} | |
private void style(AttributeSet attrs, int defStyle) { | |
if (isInEditMode()) | |
return; | |
FontFactory factory = new FontFactory(getContext()); | |
int style = getTypeface() != null ? getTypeface().getStyle() : Typeface.NORMAL; | |
Typeface tf = factory.getRegularTypeface(); | |
if (attrs != null) { | |
} | |
if (style == Typeface.BOLD) | |
tf = factory.getBoldTypeface(); | |
else if (style == Typeface.ITALIC) | |
tf = factory.getItalicTypeface(); | |
setTypeface(tf); | |
} | |
} |
Author
voghDev
commented
Nov 3, 2016
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment