多种字体样式的TextView

无图无真相,O(∩_∩)O哈哈~

这种效果一个TextView就可以搞定,多种字体样式,多种颜色,多种字体大小

废话不多说,直接上代码:


/**
  *@date on 2019/4/25
  *@author kelina
  *@describe 自定义TextView,实现同个TextView使用多种字体样式
  */
public class MultTypeFaceTextView extends android.support.v7.widget.AppCompatTextView {
    private List<CustomTypefaceSpan> customTypefaceSpans = new ArrayList<>();
    private List<String> spans= new ArrayList<>();

    public MultTypeFaceTextView(Context context) {
        super(context);
    }
 
    public MultTypeFaceTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
 
 
    }
 
    public MultTypeFaceTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    /**
     * 设置字体样式
     * @param customTypefaceSpan typeFace、文字缩放大小,颜色id
     * @param span 需要修改字体样式的文字
     */
    public void setTypeFaceSpan(CustomTypefaceSpan customTypefaceSpan,String span){
        this.customTypefaceSpans.add(customTypefaceSpan);
        this.spans.add(span);
//        setText(customTypefaceSpan.getFamily());
    }
 
    @Override
    public void setText(CharSequence text, BufferType type) {
        int index;
        if(!TextUtils.isEmpty(text) && spans!=null){
            SpannableStringBuilder ssb = new SpannableStringBuilder(text);
            for(int i=0,length = spans.size();i<length;i++){
                index = text.toString().indexOf(spans.get(i));
                if(index != -1){
                    ssb.setSpan(customTypefaceSpans.get(i),index,index+spans.get(i).length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                }
            }
            super.setText(ssb, type);
        }

    }
}
/**
 * @author kelina
 * @date on 2019/4/25
 * @describe 自定字体样式
 */
public class CustomTypefaceSpan extends TypefaceSpan {
    private Typeface typeface;
    private float scaleTextSize = 1; //字体大小倍数
    private int color = -99;//字体颜色

    /**
     * @param content TextView的内容
     * @param type    字体样式
     */
    public CustomTypefaceSpan(String content, Typeface type) {
        super(content);
        typeface = type;
    }

    /**
     * @param content       TextView的内容
     * @param type          字体样式
     * @param scaleTextSize 文字大小的倍数
     */
    public CustomTypefaceSpan(String content, Typeface type, float scaleTextSize) {
        super(content);
        typeface = type;
        this.scaleTextSize = scaleTextSize;
    }

    /**
     * @param content       TextView的内容
     * @param type          字体样式
     * @param scaleTextSize 文字大小的倍数
     * @param color         文字的颜色
     */
    public CustomTypefaceSpan(String content, Typeface type, float scaleTextSize, int color) {
        super(content);
        typeface = type;
        this.scaleTextSize = scaleTextSize;
        this.color = color;
    }

    /**
     * @param content TextView的内容
     * @param type    字体样式
     * @param color   文字的颜色
     */
    public CustomTypefaceSpan(String content, Typeface type, int color) {
        super(content);
        typeface = type;
        this.color = color;
    }

    @Override
    public void updateDrawState(TextPaint ds) {
        applyCustomTypeFace(ds);
    }

    @Override
    public void updateMeasureState(TextPaint paint) {
        applyCustomTypeFace(paint);
    }

    private void applyCustomTypeFace(Paint paint) {
        int oldStyle;
        Typeface old = paint.getTypeface();
        if (old == null) {
            oldStyle = 0;
        } else {
            oldStyle = old.getStyle();
        }

        Typeface newTf = Typeface.create(typeface, oldStyle);
        int fake = oldStyle & ~newTf.getStyle();

        if ((fake & Typeface.BOLD) != 0) {
            paint.setFakeBoldText(true);
        }

        if ((fake & Typeface.ITALIC) != 0) {
            paint.setTextSkewX(-0.25f);
        }

        paint.setTextSize(paint.getTextSize() * scaleTextSize);

        if (color != -99) {
            paint.setColor(color);
        }

        paint.setTypeface(typeface);
    }
}

调用方法:

 String s="一生一世:1314,我爱你:520,气死气死你:74740";
        MultTypeFaceTextView multtypefaceTextViewID = findViewById(R.id.multtypefaceTextViewID);
        multtypefaceTextViewID.setTypeFaceSpan(new CustomTypefaceSpan(s, Typeface.createFromAsset(getAssets(), "go.OTF"),1.8f,getResources().getColor(R.color.colorAccent)),"1314");
        multtypefaceTextViewID.setTypeFaceSpan(new CustomTypefaceSpan(s,Typeface.createFromAsset(getAssets(), "font.TTF"),1.5f,getResources().getColor(R.color.colorAccent)),"520");
        multtypefaceTextViewID.setTypeFaceSpan(new CustomTypefaceSpan(s,Typeface.createFromAsset(getAssets(), "TT1018M_.TTF"),1.0f,getResources().getColor(R.color.colorAccent)),"74740");
        multtypefaceTextViewID.setText(s);

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值