Setting typeface of hint in TextEdit

2020-02-11 03:25发布

Is it possible to set typeface of hint element of EditText view without changing the typeface of the EditText itself? Thanks.

11条回答
倾城 Initia
2楼-- · 2020-02-11 03:37

editText.setTypeface(Typeface.SERIF);

查看更多
▲ chillily
3楼-- · 2020-02-11 03:46

You should create CustomTypefaceSpan to have different fonts for Hint and EditText.

Typeface hintTypeface = Typeface.createFromAsset(getAssets(), "AguafinaScript-Regular.ttf");
TypefaceSpan typefaceSpan = new CustomTypefaceSpan(hintTypeface);
SpannableString spannableString = new SpannableString("some hint text");
spannableString.setSpan(typefaceSpan, 0, spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
editText.setHint(spannableString);


public class CustomTypefaceSpan extends TypefaceSpan
{
    private final Typeface mNewType;

    public CustomTypefaceSpan(Typeface type)
    {
        super("");
        mNewType = type;
    }

    public CustomTypefaceSpan(String family, Typeface type)
    {
        super(family);
        mNewType = type;
    }

    @Override
    public void updateDrawState(TextPaint ds)
    {
        applyCustomTypeFace(ds, mNewType);
    }

    @Override
    public void updateMeasureState(TextPaint paint)
    {
        applyCustomTypeFace(paint, mNewType);
    }

    private static void applyCustomTypeFace(Paint paint, Typeface tf)
    {
        int oldStyle;
        Typeface old = paint.getTypeface();
        if (old == null)
        {
            oldStyle = 0;
        }
        else
        {
            oldStyle = old.getStyle();
        }

        int fake = oldStyle & ~tf.getStyle();
        if ((fake & Typeface.BOLD) != 0)
        {
            paint.setFakeBoldText(true);
        }

        if ((fake & Typeface.ITALIC) != 0)
        {
            paint.setTextSkewX(-0.25f);
        }

        paint.setTypeface(tf);
    }
}
查看更多
在下西门庆
4楼-- · 2020-02-11 03:48

You can set hint font by override typefacefont

public class CustomEditText extends EditText {

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

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

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

public CustomEditText(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(), "opensansbold.ttf");
    this.setTypeface(font);
}

@Override
public void setTypeface(Typeface tf, int style) {
    tf = Typeface.createFromAsset(getContext().getAssets(), "opensansbold.ttf");
    super.setTypeface(tf, style);
}
}
查看更多
Deceive 欺骗
5楼-- · 2020-02-11 03:52

You do not have to do anything. Just simply set the typeface to the EditText, and that will automatically change the hint font as well.

Here is the custom typeface and how to set it:

Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/gobold_uplow.ttf");

Edittext mEmailEntry = (EditText) findViewById(R.id.entry_email);
mEmailEntry.setTypeface(typeface);
查看更多
再贱就再见
6楼-- · 2020-02-11 03:54

in order to have specific font there is a great library here , its very simple, but to have a edittext with android:inputType="textPassword" you should do a little more, so here it is:

  1. remove android:inputType="textPassword" from xml
  2. apply typeface using this library
  3. set passwordtransformation in code:

    password.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS); password.setTransformationMethod(PasswordTransformationMethod.getInstance());

查看更多
登录 后发表回答