How to set custom fonts on EditText in Android?

2019-02-17 11:31发布

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...

8条回答
贼婆χ
2楼-- · 2019-02-17 11:58

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();
    }
}
查看更多
太酷不给撩
3楼-- · 2019-02-17 12:03

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);
查看更多
戒情不戒烟
4楼-- · 2019-02-17 12:05

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

查看更多
该账号已被封号
5楼-- · 2019-02-17 12:07

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/>
查看更多
beautiful°
6楼-- · 2019-02-17 12:09

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!

查看更多
放荡不羁爱自由
7楼-- · 2019-02-17 12:13

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

查看更多
登录 后发表回答