双行带标签TextView

package com.jd.lib.videoimmersion.view.widget;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatTextView;
import android.text.SpannableStringBuilder;
import android.text.StaticLayout;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.ViewTreeObserver;

import com.jd.lib.common.widget.CenterAlignImageSpan;
import com.jd.lib.common.widget.TextDrawable;
import com.jingdong.corelib.utils.Log;
import com.jingdong.jdsdk.utils.DPIUtil;

import static android.text.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE;

public class LabelTextView extends AppCompatTextView {

    private CharSequence originSpannableString;

    public LabelTextView(Context context) {
        super(context);
        init();
    }

    public LabelTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

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

    private void init() {
        getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                adjustText();
                getViewTreeObserver().removeGlobalOnLayoutListener(this);
            }
        });
    }

    public void setText(String label, String color, String text) {
        if (TextUtils.isEmpty(label) || TextUtils.isEmpty(color)) {
            setText(text);
        } else {
            if (label.length() > 3) {
                label = label.trim().substring(0, 3) + "...";
            }
            try {
                int colorValue = Color.parseColor(color);
                originSpannableString = getSpan(label, colorValue, text);
                setText(getAdjustedSpannableString(originSpannableString));
            } catch (Exception e) {
                e.printStackTrace();
                setText(text);
            }
        }
    }

    private CharSequence getSpan(String label, int color, String text) {
        SpannableStringBuilder ssb = new SpannableStringBuilder( "  " + text);
        TextDrawable drawable = TextDrawable.builder()
                .beginConfig()
                .fontSize(DPIUtil.dip2px(11))
                .height(DPIUtil.dip2px(15))
                .endConfig()
                .buildRoundRect(label, color, DPIUtil.dip2px(2));
        ssb.setSpan(new CenterAlignImageSpan(drawable), 0, 1, SPAN_EXCLUSIVE_EXCLUSIVE);
        return ssb;
    }

    private void adjustText() {
        if (TextUtils.isEmpty(originSpannableString) ) return;

        final CharSequence adjustedSpannableString = getAdjustedSpannableString(originSpannableString);
        if (TextUtils.isEmpty(adjustedSpannableString)
                || TextUtils.equals(originSpannableString, adjustedSpannableString)) return;

        setText(adjustedSpannableString);
    }

    /**
     * 对齐换行:在首行末尾加换行符,否则当后面文字是较长英文单词时标签可能会消失
     * @param cs
     * @return
     */
    private CharSequence getAdjustedSpannableString(CharSequence cs) {
        final int width = getMeasuredWidth();
        if (Log.D) {
            Log.d("LabelTextView", "getAdjustedSpannableString: width = " + width);
        }
        if (width <= 0 || TextUtils.isEmpty(cs)) return cs;

        float desiredWidth = StaticLayout.getDesiredWidth(cs, getPaint());

        if (desiredWidth <= width) return cs;

        for (int i = 0; i < cs.length(); i++) {
            CharSequence line = cs.subSequence(0, i);
            float lineWidth = StaticLayout.getDesiredWidth(line, getPaint());
            if (lineWidth > width) {
                SpannableStringBuilder ssb = new SpannableStringBuilder(cs.subSequence(0, i - 1));
                ssb.append('\n').append(cs.subSequence(i, cs.length()));
                setText(ssb);
                return ssb;
            }
        }

        return cs;
    }
}

TextDrawable: 参考 https://github.com/amulyakhare/TextDrawable 注意:使用前需setBounds()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值