【问题标题】:Different typeface for parts of text部分文本的不同字体
【发布时间】:2016-06-24 20:23:09
【问题描述】:

我有两种字体,我想使用 Typeface1 作为标题,使用 Typeface2 作为正文。

有没有办法做到这一点?如果有帮助,我可以在标题周围添加标签,例如 <b>Title</b>

Typeface typeface1 = Typeface.createFromAsset(getAssets(), "fonts/Typeface1.ttf");
Typeface typeface2 = Typeface.createFromAsset(getAssets(), "fonts/Typeface2.ttf");
textView.setTypeface(typeface1);

textView.setText("<b>Title 1</b>\n" +
                "Body 1\n" +
                "<b>Title 2</b>\n" +
                "Body 2\n" +
                "<b>Title 3</b>\n" +
                "Body 3");

【问题讨论】:

    标签: android


    【解决方案1】:

    您需要使用Html.fromHtml() 在您的 XML 字符串中使用 HTML。在您的布局 XML 中简单地引用带有 HTML 的字符串是行不通的。

    例子:

    textView.setText(Html.fromHtml("<b>Title</b><br>Body1<br><b>Title2</b><br>Body2"));
    

    这可能有效

    【讨论】:

      【解决方案2】:

      尝试使用SpannableStringBuilder 中提供的this answer

      TextView txt = (TextView) findViewById(R.id.custom_fonts);  
      Typeface font = Typeface.createFromAsset(getAssets(), "font1.ttf");
      Typeface font2 = Typeface.createFromAsset(getAssets(), "font2.ttf");   
      SpannableStringBuilder spannableStringBuilder  = new SpannableStringBuilder("TextTextText");
      spannableStringBuilder.setSpan (new CustomTypefaceSpan("", font2), start, end, Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
      spannableStringBuilder.setSpan (new CustomTypefaceSpan("", font), start, end, Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
      txt.setText(SS);
      

      CustomTypefaceSpan.java

      public class CustomTypefaceSpan extends TypefaceSpan {
      
      private final Typeface newType;
      
      public CustomTypefaceSpan(String family, Typeface type) {
          super(family);
          newType = type;
      }
      
      @Override
      public void updateDrawState(TextPaint ds) {
          applyCustomTypeFace(ds, newType);
      }
      
      @Override
      public void updateMeasureState(TextPaint paint) {
          applyCustomTypeFace(paint, newType);
      }
      
      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);
      }
      }
      

      【讨论】:

        【解决方案3】:

        不建议混合使用字体和 html 标签。
        要在 textview 中使用 html 标签,请参阅(Satish Kumar 的)答案。

        textView.setText("<b>Title 1</b>\n" +
                        "Body 1\n" +
                        "<b>Title 2</b>\n" +
                        "Body 2\n" +
                        "<b>Title 3</b>\n" +
                        "Body 3");
        

        要在同一个文本视图中使用多种字体,

        TextView txt = (TextView) findViewById(R.id.custom_fonts);  
                txt.setTextSize(30);
                Typeface font = Typeface.createFromAsset(getAssets(), "Akshar.ttf");
                Typeface font2 = Typeface.createFromAsset(getAssets(), "bangla.ttf");   
                SpannableStringBuilder SS = new SpannableStringBuilder("আমারநல்வரவு");
                SS.setSpan (new CustomTypefaceSpan("", font2), 0, 4,Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
                SS.setSpan (new CustomTypefaceSpan("", font), 4, 11,Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
                txt.setText(SS);
        

        CustomTypefaceSpan 类:

        import android.graphics.Paint;
        import android.graphics.Typeface;
        import android.text.TextPaint;
        import android.text.style.TypefaceSpan;
        
            public class CustomTypefaceSpan extends TypefaceSpan {
                private final Typeface newType;
        
                public CustomTypefaceSpan(String family, Typeface type) {
                    super(family);
                    newType = type;
                }
        
                @Override
                public void updateDrawState(TextPaint ds) {
                    applyCustomTypeFace(ds, newType);
                }
        
                @Override
                public void updateMeasureState(TextPaint paint) {
                    applyCustomTypeFace(paint, newType);
                }
        
                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);
                }
            }
        


        参考资料:
        * Multiple TypeFaces
        * Span styles and custom typefaces

        【讨论】:

          猜你喜欢
          • 2014-09-15
          • 2020-03-27
          • 2019-03-22
          • 2014-01-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多