删除线,下划线,多作用域加粗修改颜色的自定义TextView(包含关键字加粗和修改颜色)持续更新

package com.example.myapplication;


import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.util.AttributeSet;

import androidx.appcompat.widget.AppCompatTextView;

public class TextViewLine extends AppCompatTextView {
    //下划线颜色
    int mUnderLineColor = Color.TRANSPARENT;
    //删除线颜色
    int mStrikeLineColor = Color.RED;
    //作用区域内text颜色
    int mTextLengthColor = Color.TRANSPARENT;
    boolean mTextBold = false;
    boolean mStrikeLine = true;
    boolean mUnderLine = false;
    int mStart = -1;
    int mEnd = -1;
    SpannableStringBuilder mStyle;

    public TextViewLine(Context context) {
        this(context, null);
    }

    private void initPaint(Context context, AttributeSet attrs) {
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.TextViewLine);
        //删除线的颜色和样式
        mUnderLineColor = array.getColor(R.styleable.TextViewLine_setUnderLineColor, Color.TRANSPARENT);
        mStrikeLineColor = array.getColor(R.styleable.TextViewLine_setStrikeLineColor, Color.RED);
        mTextLengthColor = array.getColor(R.styleable.TextViewLine_setTextLengthColor, Color.TRANSPARENT);
        mTextBold = array.getBoolean(R.styleable.TextViewLine_setLengthTextBold, false);
        mStart = array.getInt(R.styleable.TextViewLine_setTextStartColorIndex, -1);
        mEnd = array.getInt(R.styleable.TextViewLine_setTextEndColorIndex, -1);
        mStyle = new SpannableStringBuilder(this.getText().toString());
        paint = new Paint();
        paint.setColor(mStrikeLineColor);
//        paint.setStrokeJoin(Paint.Join.ROUND);
//        paint.setStrokeCap(Paint.Cap.ROUND);
        paint.setStrokeWidth(5);
        paint.setStrikeThruText(mStrikeLine);

        if (this.getTextSize() > 0 && mStart != -1 && mEnd > mStart && mEnd <= this.getTextSize()) {
            if (mTextBold) {
                mStyle.setSpan(new StyleSpan(Typeface.BOLD), mStart, mEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            if (mTextLengthColor != Color.TRANSPARENT) {
                mStyle.setSpan(new ForegroundColorSpan(mTextLengthColor), mStart, mEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                this.setText(mStyle);
            }
        }


    }

    public TextViewLine(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initPaint(context, attrs);
    }

    public TextViewLine(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    private Paint paint;


    /**
     * 设置start-end区域的textcolor和是否加粗
     * @param start             文字作用域起点
     * @param end               文字作用域终点(不包括)
     * @param TextLengthColor   文字作用域颜色
     * @param bold              文字作用域是否加粗
     */
    public void setmTextLengthColorAndBold(int start,int end,int TextLengthColor,boolean bold){
        if (getTextSize() > 0 && start>=0 && end > start && end <= getTextSize()) {
            mStyle.setSpan(new ForegroundColorSpan(TextLengthColor), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            mStyle.setSpan(new StyleSpan(Typeface.BOLD), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            this.setText(mStyle);
        }
    }

    /**
     * 根据关键字设置颜色和加粗
     * @param keyword
     * @param textColor
     * @param bold
     */
    public void setKeyWordColorAndBold(String keyword,int textColor,boolean bold){
        int index = 0;
        while (index < getTextSize()) {
            int begin = getText().toString().indexOf(keyword, index);
            if (begin == -1) break;
            int end = begin + keyword.length();
            mStyle.setSpan(new ForegroundColorSpan(textColor), begin, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            if(bold) {
                mStyle.setSpan(new StyleSpan(Typeface.BOLD), begin, end, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            index = end;
        }
        this.setText(mStyle);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        //TextView布局的高度和宽度
        float x = this.getWidth();
        float y = this.getHeight();
        //根据Textview的高度和宽度设置删除线的位置
        //四个参数的意思:起始x的位置,起始y的位置,终点x的位置,终点y的位置
        canvas.drawLine(0f, y / 2, x, y / 2, paint);
        if (mUnderLineColor != Color.TRANSPARENT) {
            paint.setStrikeThruText(false);
            paint.setUnderlineText(mUnderLine);
            paint.setColor(mUnderLineColor);
            canvas.drawLine(0f, y, x, y, paint);
        }
        //super最后调用表示删除线在位于文字的上边
        //super方法先调用删除线不显示
        super.onDraw(canvas);
    }


}

value下新建atts.xml,加入代码

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="TextViewLine">
        <attr name="setStrikeThruText" format="boolean"/>
        <attr name="setUnderLineText" format="boolean"/>
        <attr name="setLengthTextBold" format="boolean"/>
        <attr name="setStrikeLineColor" format="color"/>
        <attr name="setUnderLineColor" format="color"/>
        <attr name="setTextStartColorIndex" format="integer"/>
        <attr name="setTextEndColorIndex" format="integer"/>
        <attr name="setTextLengthColor" format="color"/>
    </declare-styleable>
</resources>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值