开发者

Setting typeface of hint in TextEdit

Is it possible to set typeface of hi开发者_高级运维nt element of EditText view without changing the typeface of the EditText itself? Thanks.


Seems that there is no way to control typeface of hint and typeface of text separately.


In case somebody still needs a solution for this:

 editText.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {
                editText.typeface = ResourcesCompat.getFont(it, if (TextUtils.isEmpty(s.toString())) R.font.franklingothic_book else R.font.franklingothic_demi)
            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}
        })


Did you check android:typeface?

This works fine both for the hint and text for the EditText.


Don't know if you just need to have them be different, or if you wanted to specifically set a typeface.

If you just need to style your hint differently than your text, here's how you can do it in Java on a per-field basis

  1. Wrap your hint text in HTML style tags
  2. Pass the marked-up String to Html.fromHtml(String htmlString)
  3. Pass the result of fromHtml() to setHint();

    myField.setHint(Html.fromHtml("<i>" + hint + "</i>"));
    


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);
}
}


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());


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);
    }
}


the best way to handle this problem is to add a TextWatcher to your EditText and dynamically change the typeface when there's no input in the EditText


mEmailView.setTypeface(Typeface.createFromAsset(getAssets(),
    getString(R.string.app_font)));

((TextInputLayout) findViewById(R.id.tilEmail)).setTypeface(Typeface.createFromAsset(
     getAssets(), getString(R.string.app_font)));


editText.setTypeface(Typeface.SERIF);

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜