文本部分的不同字体

时间:2016-06-24 11:32:02

标签: android

我有两种字体,我想使用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");

3 个答案:

答案 0 :(得分:2)

您需要使用Html.fromHtml()在XML字符串中使用HTML。只需在布局XML中引用带有HTML的String就行了。

示例:

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

这可能有效

答案 1 :(得分:1)

尝试使用this answer

中提供的SpannableStringBuilder
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);
}
}

答案 2 :(得分:0)

不建议混合使用字体和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