: setSpan (4 ... 5) ends beyond length 0

时间:2025-03-14 08:49:28
 at (:436)
    at (:163)
    at (:152)
    at (:46)
    at (:164)
    at (:108)
    at (:37)

此处亮点就是。可以实现一种你想要的模糊效果匹配

Pattern pattern = ((matchContent));

第三个参数含义

Spannable.SPAN_EXCLUSIVE_EXCLUSIVE //前后都不包括
Spannable.SPAN_INCLUSIVE_EXCLUSIVE //前包括后不包括
Spannable.SPAN_EXCLUSIVE_INCLUSIVE //前不包括后包括

/**
     *
     * @param context
     * @param spannable
     * @param allContent    内容
     * @param matchContent  匹配关键内容
     * @param foregroundColor 匹配的文字的颜色
     * @param textSize  > 0 会改变标记文字的大小
     * @return
     */
    public static Spannable addNewSpanable(Context context, Spannable spannable, String allContent, String matchContent, @ColorInt int foregroundColor, int textSize) {
        Pattern pattern = ((matchContent));
        Matcher matcher = (allContent);

        while(()) {
            int start = ();
            if (start >= 0) {
                int end = start + ();
                if (textSize > 0) {
                    (new AbsoluteSizeSpan(TDevice.sp2px(context, (float)textSize)), start, end, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
                }

                (new ForegroundColorSpan(foregroundColor), start, end, Spannable.SPAN_INCLUSIVE_INCLUSIVE );
            }
        }

        return spannable;
    }

调用

		Spannable spannable = null;
		if (!(content) && !(mSearchKey)) {
            spannable = ().newSpannable(content);
            forcolor = ("#FF3300");
            spannable = (mContext, spannable, content, mSearchKey, forcolor, 0);
        }
        (.tv_message_sub_title, spannable == null ? content : spannable);