How to set custom fonts on EditText in Android?

2019-02-17 11:54发布

问题:

I am trying to implement a custom typeface on an EditText. Does anyone have a better approach as opposed to what I'm currently doing?

Typeface myFont = Typeface.createFromAsset(getAssets(), "fonts/myfont.ttf");
edittext.setTypeface(myFont);

Because I have lots of EditTexts...

回答1:

public class CEditText extends EditText {


    private Context context;
    private AttributeSet attrs;
    private int defStyle;

    public CEditText(Context context) {
        super(context);
        this.context=context;
        init();
    } 

     public CEditText(Context context, AttributeSet attrs) {
          super(context, attrs);
          this.context=context;
          this.attrs=attrs;
          init();
     }

    public CEditText(Context context, AttributeSet attrs, int defStyle) {
          super(context, attrs, defStyle);
          this.context=context;
          this.attrs=attrs;
          this.defStyle=defStyle;
          init();
    }

    private void init() {
          Typeface font=Typeface.createFromAsset(getContext().getAssets(), "fonts/myfont.ttf");
          this.setTypeface(font);
    }
    @Override
    public void setTypeface(Typeface tf, int style) {
        tf=Typeface.createFromAsset(getContext().getAssets(), "fonts/myfont.ttf");
        super.setTypeface(tf, style);
    }

    @Override
    public void setTypeface(Typeface tf) {
        tf=Typeface.createFromAsset(getContext().getAssets(), "fonts/myfont.ttf");
        super.setTypeface(tf);
    }

call this class in XML as follows

<yourpackagename.CEditText  android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp">
</yourpackagename.CEditText>


回答2:

Create a new class that extends EditText like

public class CustomEditTextNormal extends EditText
{

  public CustomEditTextNormal(Context context)
  {
      super(context);
      init(context);
  }

  public CustomEditTextNormal(Context context, AttributeSet attrs)
  {
    super(context, attrs);
    init(context);
  }

  public CustomEditTextNormal(Context context, AttributeSet attrs, int defStyle)
  {
    super(context, attrs, defStyle);
    init(context);
  }

  protected void onDraw(Canvas canvas)
  {
    super.onDraw(canvas);
  }

  public void init(Context context)
  {
    try
    {
        Typeface myFont = Typeface.createFromAsset(context.getAssets(), "fonts/myfont.ttf");

        setTypeface(mSearchAndSend.HelveticaLight);
    }
    catch (Exception e)
    {
        Logger.LogError(e);
    }
  }
}

and include it on your XML like

<com.package.name.CustomEditText/>


回答3:

How about create a new class that inherits EditText and sets the typeface you want and then instantiate the new class in the xml?



回答4:

Create a new class which inherits from EditText, and then override the public void setTypeface(Typeface tf, int style) method and add your own font.



回答5:

You need to make one method in Common class Like,

public void setExternalFonts(EdiText tv) {
  Typeface tf = Typeface.createFromAsset(context.getAssets(),
            "fonts/myfont.ttf");
  tv.setTypeface(tf);
}

Now call this method as

yourClassName.setExternalFonts(yourEditText);


回答6:

Create a new class which inherits from EditText, and then override the public void setTypeface(Typeface tf, int style) method and add your own font.

And use in your activity like this FontLoader.setQuickSandTypeface(YourEditText) in your activity



回答7:

try this one

public void overrideFonts(final Context context, final View v) {
    try {
        if (v instanceof ViewGroup) {
            ViewGroup vg = (ViewGroup) v;
            for (int i = 0; i < vg.getChildCount(); i++) {
                View child = vg.getChildAt(i);
                overrideFonts(context, child);
            }
        } else if (v instanceof EditText) {
            ((EditText) v).setTypeface(Typeface.createFromAsset(context.getAssets(), "roboto_thin.ttf"));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}


回答8:

Try this. Also useful for Buttons, TextViews.. whatever!

<your.namespace.app.FontEditText
     app:font="montserrat"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"/>

Who is possible? By this way!

public class FontEditText extends EditText {

public FontEditText(Context context) {
    this( context, null );
}

public FontEditText(Context context, AttributeSet attrs) {
    this( context, attrs, 0 );
    init( context, attrs );
}

public FontEditText(Context context, AttributeSet attrs, int defStyle) {
    super( context, attrs, defStyle );
    init( context, attrs );
}

public FontEditText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super( context, attrs, defStyleAttr, defStyleRes );
    init( context, attrs );
}

private void init(Context context, AttributeSet attrs) {
    TypedArray ta = context.obtainStyledAttributes( attrs, R.styleable.Fonts );

    if ( ta != null ) {
        String fontAsset = ta.getString( R.styleable.Fonts_font );
        if ( !StringUtils.isEmpty( fontAsset ) ) {
            int type = Integer.parseInt( fontAsset );

            Typeface typeFace = FontManager.getInstance( context ).getByType( type );
            ta.recycle();
            super.setTypeface( typeFace );
        }
    }
}

}

public class FontManager {

private static FontManager Instance;

private Context context;

private Typeface robotoCondensedBold;
private Typeface robotoCondensed;
private Typeface robotoLight;
private Typeface kronica;
private Typeface montserrat;
private Typeface montserratLight;
private Typeface keepCalmMedium;

private FontManager(Context context) {
    this.context = context;
    this.robotoCondensedBold = Typeface.createFromAsset( context.getAssets(), "fonts/RobotoCondensed-Bold.ttf" );
    this.robotoCondensed = Typeface.createFromAsset( context.getAssets(), "fonts/RobotoCondensed-Regular.ttf" );
    this.robotoLight = Typeface.createFromAsset( context.getAssets(), "fonts/Roboto-Light.ttf" );
    this.kronica = Typeface.createFromAsset( context.getAssets(), "fonts/kronika.ttf" );
    this.montserrat = Typeface.createFromAsset( context.getAssets(), "fonts/Montserrat-Regular.ttf" );
    this.montserratLight = Typeface.createFromAsset( context.getAssets(), "fonts/Montserrat-Light.ttf" );
    this.keepCalmMedium = Typeface.createFromAsset( context.getAssets(), "fonts/KeepCalmMedium.ttf" );
}

public synchronized static FontManager getInstance(Context context) {
    if ( Instance == null )
        Instance = new FontManager( context );

    return Instance;
}

public Typeface getByType(int type) {
    switch ( type ) {
        case 0:
            return FontManager.getInstance( context ).getRobotoCondensedBold();
        case 1:
            return FontManager.getInstance( context ).getRobotoLight();
        case 2:
            return FontManager.getInstance( context ).getKronica();
        case 3:
            return FontManager.getInstance( context ).getRobotoCondensed();
        case 4:
            return FontManager.getInstance( context ).getMontserrat();
        case 5:
            return FontManager.getInstance( context ).getMontserratLight();
        case 6:
            return FontManager.getInstance( context ).getKeepCalmMedium();
        default:
            return Typeface.DEFAULT;
    }
}

public Typeface getRobotoCondensedBold() {
    return robotoCondensedBold;
}

public Typeface getKronica() {
    return kronica;
}

public Typeface getRobotoCondensed() {
    return robotoCondensed;
}

public Typeface getRobotoLight() {
    return robotoLight;
}

public Typeface getMontserrat() {
    return montserrat;
}

public Typeface getMontserratLight() {
    return montserratLight;
}

public Typeface getKeepCalmMedium() {
    return keepCalmMedium;
}

In addition, a font_attrs.xml in your res folder:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="Fonts">
        <attr name="font" format="enum">
            <enum name="robotoCondensedBold" value="0"/>
            <enum name="robotoLight" value="1"/>
            <enum name="kronica" value="2"/>
            <enum name="robotoCondensed" value="3"/>
            <enum name="montserrat" value="4"/>
            <enum name="montserratLight" value="5"/>
            <enum name="keepCalmMedium" value="6"/>
        </attr>
    </declare-styleable>
</resources> 

Note that you only need to modify the FontManager and the font_attrs.xml to customize your fonts!