【问题标题】:Setting typeface of hint in TextEdit在 TextEdit 中设置提示字体
【发布时间】:2011-06-10 15:48:19
【问题描述】:

是否可以在不更改 EditText 本身的字体的情况下设置 EditText 视图的提示元素的字体?谢谢。

【问题讨论】:

标签: android user-interface view


【解决方案1】:

好像没有办法分别控制提示的字体和文本的字体。

【讨论】:

  • 今天仍然如此吗?
【解决方案2】:

你检查android:typeface了吗?

这对于 EditText 的提示和文本都适用。

【讨论】:

  • 是的,它会影响提示和文本。有什么简单的方法来改变唯一的提示吗?
【解决方案3】:

不知道您是否只是需要让它们不同,或者您是否想专门设置字体。

如果您只需要设置与文本不同的提示样式,以下是在 Java 中基于每个字段的方法

  1. 将提示文本包装在 HTML 样式标签中
  2. 将标记的字符串传递给 Html.fromHtml(String htmlString)
  3. 将fromHtml()的结果传递给setHint();

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

【讨论】:

    【解决方案4】:

    如果有人仍然需要解决这个问题:

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

    【讨论】:

      【解决方案5】:

      您可以通过覆盖字体字体来设置提示字体

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

      【讨论】:

        【解决方案6】:

        为了拥有特定的字体,有一个很棒的库 here ,它非常简单,但是要拥有一个带有 android:inputType="textPassword" 的编辑文本,你应该做更多的事情,所以这里是:

        1. 从 xml 中删除 android:inputType="textPassword"
        2. 使用此library 应用字体
        3. 在代码中设置密码转换:

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

        【讨论】:

          【解决方案7】:

          您应该创建 CustomTypefaceSpan 以使 Hint 和 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);
              }
          }
          

          【讨论】:

            【解决方案8】:

            处理此问题的最佳方法是在 EditText 中添加一个 TextWatcher,并在 EditText 中没有输入时动态更改字体

            【讨论】:

              【解决方案9】:
              mEmailView.setTypeface(Typeface.createFromAsset(getAssets(),
                  getString(R.string.app_font)));
              
              ((TextInputLayout) findViewById(R.id.tilEmail)).setTypeface(Typeface.createFromAsset(
                   getAssets(), getString(R.string.app_font)));
              

              【讨论】:

              • 您能对此代码添加一些解释吗?另外,这与Shahid's answer 有何不同?
              • 您必须同时为 TextInputLayout 和 EditText 设置字体。不仅适用于 EditText
              【解决方案10】:

              editText.setTypeface(Typeface.SERIF);

              【讨论】:

              • editText.setTypeface(Typeface.SERIF);用它来改变edittext的字体类型
              猜你喜欢
              • 2021-05-03
              • 1970-01-01
              • 2015-03-27
              • 1970-01-01
              • 2015-09-23
              • 2010-10-26
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多