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()