17 | * Typeface management is regulated through {@link FontUtil}. 18 | *
19 | * 20 | * @author Pixplicity 21 | */ 22 | @TargetApi(3) 23 | public class FontAutoCompleteTextView extends AutoCompleteTextView { 24 | 25 | public FontAutoCompleteTextView(Context context) { 26 | super(context); 27 | } 28 | 29 | public FontAutoCompleteTextView(Context context, AttributeSet attrs) { 30 | super(context, attrs); 31 | setCustomTypeface(attrs, android.R.attr.autoCompleteTextViewStyle); 32 | } 33 | 34 | public FontAutoCompleteTextView(Context context, AttributeSet attrs, 35 | int defStyle) { 36 | super(context, attrs, defStyle); 37 | setCustomTypeface(attrs, defStyle); 38 | } 39 | public void setCustomTypeface(String font) { 40 | final Typeface tf = FontUtil.getTypeface(getContext(), font); 41 | setCustomTypeface(tf); 42 | } 43 | 44 | private void setCustomTypeface(AttributeSet attrs, int defStyle) { 45 | final Typeface tf = FontUtil.getTypeface(getContext(), attrs, defStyle); 46 | setCustomTypeface(tf); 47 | } 48 | 49 | private void setCustomTypeface(Typeface tf) { 50 | setPaintFlags(getPaintFlags() | Paint.SUBPIXEL_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG); 51 | setTypeface(tf); 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /library/src/main/java/com/pixplicity/fontview/FontButton.java: -------------------------------------------------------------------------------- 1 | package com.pixplicity.fontview; 2 | 3 | import android.annotation.TargetApi; 4 | import android.content.Context; 5 | import android.graphics.Paint; 6 | import android.graphics.Typeface; 7 | import android.util.AttributeSet; 8 | import android.widget.Button; 9 | 10 | import com.pixplicity.fontview.utils.FontUtil; 11 | 12 | 13 | /** 14 | * Extension of {@link Button} to cope with custom typefaces. Specify the desired 15 | * font using the 16 | * {@code font="myfont.ttf"} attribute, or specify it directly using {@link #setCustomTypeface(String)}. 17 | *18 | * Typeface management is regulated through {@link FontUtil}. 19 | *
20 | * 21 | * @author Pixplicity 22 | */ 23 | @TargetApi(3) 24 | public class FontButton extends Button { 25 | 26 | public FontButton(Context context) { 27 | super(context); 28 | } 29 | 30 | public FontButton(Context context, AttributeSet attrs) { 31 | super(context, attrs); 32 | setCustomTypeface(attrs, android.R.attr.buttonStyle); 33 | } 34 | 35 | public FontButton(Context context, AttributeSet attrs, int defStyle) { 36 | super(context, attrs, defStyle); 37 | setCustomTypeface(attrs, defStyle); 38 | } 39 | 40 | public void setCustomTypeface(String font) { 41 | final Typeface tf = FontUtil.getTypeface(getContext(), font); 42 | setCustomTypeface(tf); 43 | } 44 | 45 | private void setCustomTypeface(AttributeSet attrs, int defStyle) { 46 | final Typeface tf = FontUtil.getTypeface(getContext(), attrs, defStyle); 47 | setCustomTypeface(tf); 48 | } 49 | 50 | private void setCustomTypeface(Typeface tf) { 51 | setPaintFlags(getPaintFlags() | Paint.SUBPIXEL_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG); 52 | setTypeface(tf); 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /library/src/main/java/com/pixplicity/fontview/FontCheckBox.java: -------------------------------------------------------------------------------- 1 | package com.pixplicity.fontview; 2 | 3 | import android.annotation.TargetApi; 4 | import android.content.Context; 5 | import android.graphics.Paint; 6 | import android.graphics.Typeface; 7 | import android.util.AttributeSet; 8 | import android.widget.CheckBox; 9 | 10 | import com.pixplicity.fontview.utils.FontUtil; 11 | 12 | 13 | /** 14 | * Extension of {@link CheckBox} to cope with custom typefaces. Specify the desired 15 | * font using the 16 | * {@code font="myfont.ttf"} attribute, or specify it directly using {@link #setCustomTypeface(String)}. 17 | *18 | * Typeface management is regulated through {@link FontUtil}. 19 | *
20 | * 21 | * @author Pixplicity 22 | */ 23 | @TargetApi(3) 24 | public class FontCheckBox extends CheckBox { 25 | 26 | public FontCheckBox(Context context) { 27 | super(context, null, 0); 28 | } 29 | 30 | public FontCheckBox(Context context, AttributeSet attrs) { 31 | super(context, attrs); 32 | setCustomTypeface(attrs, android.R.attr.checkboxStyle); 33 | } 34 | 35 | public FontCheckBox(Context context, AttributeSet attrs, int defStyle) { 36 | super(context, attrs, defStyle); 37 | setCustomTypeface(attrs, defStyle); 38 | } 39 | 40 | public void setCustomTypeface(String font) { 41 | final Typeface tf = FontUtil.getTypeface(getContext(), font); 42 | setCustomTypeface(tf); 43 | } 44 | 45 | private void setCustomTypeface(AttributeSet attrs, int defStyle) { 46 | final Typeface tf = FontUtil.getTypeface(getContext(), attrs, defStyle); 47 | setCustomTypeface(tf); 48 | } 49 | 50 | private void setCustomTypeface(Typeface tf) { 51 | setPaintFlags(getPaintFlags() | Paint.SUBPIXEL_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG); 52 | setTypeface(tf); 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /library/src/main/java/com/pixplicity/fontview/FontCheckedTextView.java: -------------------------------------------------------------------------------- 1 | package com.pixplicity.fontview; 2 | 3 | import android.annotation.TargetApi; 4 | import android.content.Context; 5 | import android.graphics.Paint; 6 | import android.graphics.Typeface; 7 | import android.util.AttributeSet; 8 | import android.widget.CheckedTextView; 9 | 10 | import com.pixplicity.fontview.utils.FontUtil; 11 | 12 | 13 | /** 14 | * Extension of {@link CheckedTextView} to cope with custom typefaces. Specify the desired font using the 15 | * {@code font="myfont.ttf"} attribute, or specify it directly using {@link #setCustomTypeface(String)}. 16 | *17 | * Typeface management is regulated through {@link FontUtil}. 18 | *
19 | * 20 | * @author Pixplicity 21 | */ 22 | @TargetApi(3) 23 | public class FontCheckedTextView extends CheckedTextView { 24 | 25 | public FontCheckedTextView(Context context) { 26 | super(context); 27 | } 28 | 29 | public FontCheckedTextView(Context context, AttributeSet attrs) { 30 | super(context, attrs); 31 | setCustomTypeface(attrs, R.attr.checkedTextViewStyle); 32 | } 33 | 34 | public FontCheckedTextView(Context context, AttributeSet attrs, int defStyle) { 35 | super(context, attrs, defStyle); 36 | setCustomTypeface(attrs, defStyle); 37 | } 38 | 39 | public void setCustomTypeface(String font) { 40 | final Typeface tf = FontUtil.getTypeface(getContext(), font); 41 | setCustomTypeface(tf); 42 | } 43 | 44 | private void setCustomTypeface(AttributeSet attrs, int defStyle) { 45 | final Typeface tf = FontUtil.getTypeface(getContext(), attrs, defStyle); 46 | setCustomTypeface(tf); 47 | } 48 | 49 | private void setCustomTypeface(Typeface tf) { 50 | setPaintFlags(getPaintFlags() | Paint.SUBPIXEL_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG); 51 | setTypeface(tf); 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /library/src/main/java/com/pixplicity/fontview/FontCompoundButton.java: -------------------------------------------------------------------------------- 1 | package com.pixplicity.fontview; 2 | 3 | import android.content.Context; 4 | import android.graphics.Paint; 5 | import android.graphics.Typeface; 6 | import android.util.AttributeSet; 7 | import android.widget.CompoundButton; 8 | 9 | import com.pixplicity.fontview.utils.FontUtil; 10 | 11 | public class FontCompoundButton extends CompoundButton { 12 | 13 | public FontCompoundButton(Context context) { 14 | super(context); 15 | } 16 | 17 | public FontCompoundButton(Context context, AttributeSet attrs) { 18 | super(context, attrs); 19 | setCustomFont(attrs, android.R.attr.buttonStyle); 20 | } 21 | 22 | public FontCompoundButton(Context context, AttributeSet attrs, int defStyle) { 23 | super(context, attrs, defStyle); 24 | setCustomTypeface(attrs, defStyle); 25 | } 26 | 27 | private void setCustomFont(AttributeSet attrs, int defStyle) { 28 | FontUtil.getTypeface(getContext(), attrs, defStyle); 29 | } 30 | 31 | public void setCustomTypeface(String font) { 32 | final Typeface tf = FontUtil.getTypeface(getContext(), font); 33 | setCustomTypeface(tf); 34 | } 35 | 36 | private void setCustomTypeface(AttributeSet attrs, int defStyle) { 37 | final Typeface tf = FontUtil.getTypeface(getContext(), attrs, defStyle); 38 | setCustomTypeface(tf); 39 | } 40 | 41 | private void setCustomTypeface(Typeface tf) { 42 | setPaintFlags(getPaintFlags() | Paint.SUBPIXEL_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG); 43 | setTypeface(tf); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /library/src/main/java/com/pixplicity/fontview/FontEditText.java: -------------------------------------------------------------------------------- 1 | package com.pixplicity.fontview; 2 | 3 | import android.annotation.TargetApi; 4 | import android.content.Context; 5 | import android.graphics.Paint; 6 | import android.graphics.Typeface; 7 | import android.util.AttributeSet; 8 | import android.widget.EditText; 9 | 10 | import com.pixplicity.fontview.utils.FontUtil; 11 | 12 | /** 13 | * Extension of {@link EditText} to cope with custom typefaces. Specify the desired 14 | * font using the 15 | * {@code font="myfont.ttf"} attribute, or specify it directly using {@link #setCustomTypeface(String)}. 16 | *17 | * Typeface management is regulated through {@link FontUtil}. 18 | *
19 | * 20 | * @author Pixplicity 21 | */ 22 | @TargetApi(3) 23 | public class FontEditText extends EditText { 24 | 25 | public FontEditText(Context context) { 26 | super(context, null, 0); 27 | } 28 | 29 | public FontEditText(Context context, AttributeSet attrs) { 30 | super(context, attrs); 31 | setCustomTypeface(attrs, android.R.attr.editTextStyle); 32 | } 33 | 34 | public FontEditText(Context context, AttributeSet attrs, int defStyle) { 35 | super(context, attrs, defStyle); 36 | setCustomTypeface(attrs, defStyle); 37 | } 38 | 39 | public void setCustomTypeface(String font) { 40 | final Typeface tf = FontUtil.getTypeface(getContext(), font); 41 | setCustomTypeface(tf); 42 | } 43 | 44 | private void setCustomTypeface(AttributeSet attrs, int defStyle) { 45 | final Typeface tf = FontUtil.getTypeface(getContext(), attrs, defStyle); 46 | setCustomTypeface(tf); 47 | } 48 | 49 | private void setCustomTypeface(Typeface tf) { 50 | setPaintFlags(getPaintFlags() | Paint.SUBPIXEL_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG); 51 | setTypeface(tf); 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /library/src/main/java/com/pixplicity/fontview/FontMultiAutoCompleteTextView.java: -------------------------------------------------------------------------------- 1 | package com.pixplicity.fontview; 2 | 3 | import android.content.Context; 4 | import android.graphics.Paint; 5 | import android.graphics.Typeface; 6 | import android.util.AttributeSet; 7 | import android.widget.MultiAutoCompleteTextView; 8 | 9 | import com.pixplicity.fontview.utils.FontUtil; 10 | 11 | public class FontMultiAutoCompleteTextView extends MultiAutoCompleteTextView { 12 | 13 | public FontMultiAutoCompleteTextView(Context context) { 14 | super(context); 15 | } 16 | 17 | public FontMultiAutoCompleteTextView(Context context, AttributeSet attrs) { 18 | super(context, attrs); 19 | setCustomTypeface(attrs, android.R.attr.autoCompleteTextViewStyle); 20 | } 21 | 22 | public FontMultiAutoCompleteTextView(Context context, AttributeSet attrs, int defStyle) { 23 | super(context, attrs, defStyle); 24 | setCustomTypeface(attrs, defStyle); 25 | } 26 | 27 | public void setCustomTypeface(String font) { 28 | final Typeface tf = FontUtil.getTypeface(getContext(), font); 29 | setCustomTypeface(tf); 30 | } 31 | 32 | private void setCustomTypeface(AttributeSet attrs, int defStyle) { 33 | final Typeface tf = FontUtil.getTypeface(getContext(), attrs, defStyle); 34 | setCustomTypeface(tf); 35 | } 36 | 37 | private void setCustomTypeface(Typeface tf) { 38 | setPaintFlags(getPaintFlags() | Paint.SUBPIXEL_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG); 39 | setTypeface(tf); 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /library/src/main/java/com/pixplicity/fontview/FontRadioButton.java: -------------------------------------------------------------------------------- 1 | package com.pixplicity.fontview; 2 | 3 | import android.annotation.TargetApi; 4 | import android.content.Context; 5 | import android.graphics.Paint; 6 | import android.graphics.Typeface; 7 | import android.util.AttributeSet; 8 | import android.widget.RadioButton; 9 | 10 | import com.pixplicity.fontview.utils.FontUtil; 11 | 12 | /** 13 | * Extension of {@link android.widget.CheckBox} to cope with custom typefaces. Specify the desired 14 | * font using the 15 | * {@code font="myfont.ttf"} attribute, or specify it directly using {@link #setCustomTypeface(String)}. 16 | *17 | * Typeface management is regulated through {@link FontUtil}. 18 | *
19 | * 20 | * @author Pixplicity 21 | */ 22 | @TargetApi(3) 23 | public class FontRadioButton extends RadioButton { 24 | 25 | public FontRadioButton(Context context) { 26 | super(context); 27 | } 28 | 29 | public FontRadioButton(Context context, AttributeSet attrs) { 30 | super(context, attrs); 31 | setCustomTypeface(attrs, android.R.attr.radioButtonStyle); 32 | } 33 | 34 | public FontRadioButton(Context context, AttributeSet attrs, int defStyle) { 35 | super(context, attrs, defStyle); 36 | setCustomTypeface(attrs, defStyle); 37 | } 38 | 39 | public void setCustomTypeface(String font) { 40 | final Typeface tf = FontUtil.getTypeface(getContext(), font); 41 | setCustomTypeface(tf); 42 | } 43 | 44 | private void setCustomTypeface(AttributeSet attrs, int defStyle) { 45 | final Typeface tf = FontUtil.getTypeface(getContext(), attrs, defStyle); 46 | setCustomTypeface(tf); 47 | } 48 | 49 | private void setCustomTypeface(Typeface tf) { 50 | setPaintFlags(getPaintFlags() | Paint.SUBPIXEL_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG); 51 | setTypeface(tf); 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /library/src/main/java/com/pixplicity/fontview/FontTextView.java: -------------------------------------------------------------------------------- 1 | package com.pixplicity.fontview; 2 | 3 | import android.annotation.TargetApi; 4 | import android.content.Context; 5 | import android.graphics.Paint; 6 | import android.graphics.Typeface; 7 | import android.util.AttributeSet; 8 | import android.widget.TextView; 9 | 10 | import com.pixplicity.fontview.utils.FontUtil; 11 | 12 | /** 13 | * Extension of {@link TextView} to cope with custom typefaces. Specify the desired font using the 14 | * {@code font="myfont.ttf"} attribute, or specify it directly using {@link #setCustomTypeface(String)}. 15 | *16 | * Typeface management is regulated through {@link FontUtil}. 17 | *
18 | * 19 | * @author Pixplicity 20 | */ 21 | @TargetApi(3) 22 | public class FontTextView extends TextView { 23 | 24 | public FontTextView(Context context) { 25 | super(context); 26 | } 27 | 28 | public FontTextView(Context context, AttributeSet attrs) { 29 | super(context, attrs); 30 | setCustomTypeface(attrs, android.R.attr.textViewStyle); 31 | } 32 | 33 | public FontTextView(Context context, AttributeSet attrs, int defStyle) { 34 | super(context, attrs, defStyle); 35 | setCustomTypeface(attrs, defStyle); 36 | } 37 | 38 | public void setCustomTypeface(String font) { 39 | final Typeface tf = FontUtil.getTypeface(getContext(), font); 40 | setCustomTypeface(tf); 41 | } 42 | 43 | private void setCustomTypeface(AttributeSet attrs, int defStyle) { 44 | final Typeface tf = FontUtil.getTypeface(getContext(), attrs, defStyle); 45 | setCustomTypeface(tf); 46 | } 47 | 48 | private void setCustomTypeface(Typeface tf) { 49 | setPaintFlags(getPaintFlags() | Paint.SUBPIXEL_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG); 50 | setTypeface(tf); 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /library/src/main/java/com/pixplicity/fontview/FontToggleButton.java: -------------------------------------------------------------------------------- 1 | package com.pixplicity.fontview; 2 | 3 | import android.content.Context; 4 | import android.graphics.Paint; 5 | import android.graphics.Typeface; 6 | import android.util.AttributeSet; 7 | import android.widget.ToggleButton; 8 | 9 | import com.pixplicity.fontview.utils.FontUtil; 10 | 11 | public class FontToggleButton extends ToggleButton { 12 | 13 | public FontToggleButton(Context context) { 14 | super(context); 15 | } 16 | 17 | public FontToggleButton(Context context, AttributeSet attrs) { 18 | super(context, attrs); 19 | setCustomTypeface(attrs, android.R.attr.buttonStyleToggle); 20 | } 21 | 22 | public FontToggleButton(Context context, AttributeSet attrs, int defStyle) { 23 | super(context, attrs, defStyle); 24 | setCustomTypeface(attrs, defStyle); 25 | } 26 | 27 | public void setCustomTypeface(String font) { 28 | final Typeface tf = FontUtil.getTypeface(getContext(), font); 29 | setCustomTypeface(tf); 30 | } 31 | 32 | private void setCustomTypeface(AttributeSet attrs, int defStyle) { 33 | final Typeface tf = FontUtil.getTypeface(getContext(), attrs, defStyle); 34 | setCustomTypeface(tf); 35 | } 36 | 37 | private void setCustomTypeface(Typeface tf) { 38 | setPaintFlags(getPaintFlags() | Paint.SUBPIXEL_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG); 39 | setTypeface(tf); 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /library/src/main/java/com/pixplicity/fontview/utils/FontSpan.java: -------------------------------------------------------------------------------- 1 | package com.pixplicity.fontview.utils; 2 | 3 | import android.graphics.Paint; 4 | import android.graphics.Typeface; 5 | import android.text.TextPaint; 6 | import android.text.style.MetricAffectingSpan; 7 | 8 | /** 9 | * Span that applies a Typeface to a String 10 | */ 11 | public class FontSpan extends MetricAffectingSpan { 12 | 13 | private final Typeface typeface; 14 | 15 | public FontSpan(final Typeface typeface) { 16 | this.typeface = typeface; 17 | } 18 | 19 | @Override 20 | public void updateDrawState(final TextPaint drawState) { 21 | apply(drawState); 22 | } 23 | 24 | @Override 25 | public void updateMeasureState(final TextPaint paint) { 26 | apply(paint); 27 | } 28 | 29 | private void apply(final Paint paint) { 30 | final Typeface oldTypeface = paint.getTypeface(); 31 | final int oldStyle = oldTypeface != null ? oldTypeface.getStyle() : 0; 32 | final int fakeStyle = oldStyle & ~typeface.getStyle(); 33 | 34 | if ((fakeStyle & Typeface.BOLD) != 0) { 35 | paint.setFakeBoldText(true); 36 | } 37 | 38 | if ((fakeStyle & Typeface.ITALIC) != 0) { 39 | paint.setTextSkewX(-0.25f); 40 | } 41 | 42 | paint.setTypeface(typeface); 43 | } 44 | } -------------------------------------------------------------------------------- /library/src/main/java/com/pixplicity/fontview/utils/FontSpinnerAdapter.java: -------------------------------------------------------------------------------- 1 | package com.pixplicity.fontview.utils; 2 | 3 | import android.content.Context; 4 | import android.graphics.Typeface; 5 | import android.support.annotation.ArrayRes; 6 | import android.support.annotation.LayoutRes; 7 | import android.support.annotation.NonNull; 8 | import android.view.View; 9 | import android.view.ViewGroup; 10 | import android.widget.ArrayAdapter; 11 | import android.widget.TextView; 12 | 13 | import com.pixplicity.fontview.R; 14 | 15 | /** 16 | * A simple ArrayAdapter meant to apply the default typeface to Spinners 17 | */ 18 | public class FontSpinnerAdapter extends ArrayAdapter