将两种不同的字体样式应用于 TextView [英] Apply two different font styles to a TextView

查看:19
本文介绍了将两种不同的字体样式应用于 TextView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对单个 TextView 中的文本应用两种不同的字体样式.

我的情况和:

import android.graphics.Paint;导入 android.graphics.Typeface;导入 android.text.TextPaint;导入 android.text.style.TypefaceSpan;公共类 CustomTypefaceSpan 扩展 TypefaceSpan {私人最终字体 newType;公共自定义字体跨度(字符串系列,字体类型){超级(家庭);新类型 = 类型;}@覆盖公共无效updateDrawState(TextPaint ds){applyCustomTypeFace(ds, newType);}@覆盖公共无效updateMeasureState(TextPaint油漆){applyCustomTypeFace(paint, newType);}私有静态无效 applyCustomTypeFace(油漆涂料,字体 tf){int oldStyle;Typeface old = paint.getTypeface();如果(旧 == 空){旧样式 = 0;} 别的 {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);}}

然后当你想使用两种不同的字体时调用:

String firstWord = "first";String secondWord = "第二";//用两个字符串创建一个新的spannableSpannable spannable = new SpannableString(firstWord+secondWord);//将自定义字体设置为跨越可跨越对象的一部分spannable.setSpan(new CustomTypefaceSpan("sans-serif",CUSTOM_TYPEFACE), 0, firstWord.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);spannable.setSpan( new CustomTypefaceSpan("sans-serif",SECOND_CUSTOM_TYPEFACE), firstWord.length(), firstWord.length() + secondWord.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);//使用spannable 对象设置textView 的文本textView.setText(spannable);

I want to apply two different font styles to a text in a single TextView.

My case is same as Android - two sentences, two styles, one TextView. The only difference is that I want to set a Custom Font on the whole text. I have included Helvetica Font as an assets in my project and want to apply that font to the TextView with first part of the text will be Helvetica BOLD and remaining part Helvetica NORMAL. Any suggestions how it can be done ?

Text needed in following format. Custom text with different styles and single textview.

解决方案

One way to do this is to extend TypefaceSpan:

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

Then when you want to use two different typefaces call:

String firstWord = "first ";
String secondWord = "second";

// Create a new spannable with the two strings
Spannable spannable = new SpannableString(firstWord+secondWord);

// Set the custom typeface to span over a section of the spannable object
spannable.setSpan( new CustomTypefaceSpan("sans-serif",CUSTOM_TYPEFACE), 0, firstWord.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable.setSpan( new CustomTypefaceSpan("sans-serif",SECOND_CUSTOM_TYPEFACE), firstWord.length(), firstWord.length() + secondWord.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

// Set the text of a textView with the spannable object
textView.setText( spannable );

这篇关于将两种不同的字体样式应用于 TextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆