Make writing a habit together! This is the 12th day of my participation in the “Gold Digging Day New Plan · April More text Challenge”. Click here for more details.

preface

In Android development, we often encounter multiple styles of text, such as parts of bold, parts of color, in addition to multiple TextView concatenation we generally have two ways to handle this need.

1.HtmlCompat

Htmlcompat.fromhtml () is an easy way to change color, bold font and other requirements, it has two parameters, the first is a string with style, contains HTML tags, the second parameter is some rules for parsing string flag, mainly has the following values:

@IntDef(value = {
        FROM_HTML_SEPARATOR_LINE_BREAK_PARAGRAPH,
        FROM_HTML_SEPARATOR_LINE_BREAK_HEADING,
        FROM_HTML_SEPARATOR_LINE_BREAK_LIST_ITEM,
        FROM_HTML_SEPARATOR_LINE_BREAK_LIST,
        FROM_HTML_SEPARATOR_LINE_BREAK_DIV,
        FROM_HTML_SEPARATOR_LINE_BREAK_BLOCKQUOTE,
        FROM_HTML_OPTION_USE_CSS_COLORS,
        FROM_HTML_MODE_COMPACT,
        FROM_HTML_MODE_LEGACY
}, flag = true)
@RestrictTo(LIBRARY_GROUP_PREFIX)
@Retention(SOURCE)
@interface FromHtmlFlags {
}
Copy the code

The text in the FROM_HTML_SEPARATOR_LINE_BREAK_HEADING: H1-H6 tag is separated from the rest of the text by a line break character. FROM_HTML_SEPARATOR_LINE_BREAK_PARAGRAPH comes with a line break

Other flags that contain LINE_BREAK also handle line breaks for various labels, such as LI, UL, etc. The following will be used briefly

private fun formatSpanString(): Spanned {val htmlStr2 = ("<span>" + // large font - Newline "<h3><font color='#000000'> </font></h3>" + // Newline "<p><font Color ='#90EE90'> < span style = "max-width: 100%; </font></p>" + // normal font "<font color='#FF00FF'> Billows of billows and billows. </font><br>" + // bold "<strong><font color='#3CB371'> <font color='#3CB371'> </font></strong><br>" <em><font color='#FF4500'> <font color='#FF4500'> < / font > < / em > < br > "+ with / / underline" < u > < font color = '# 8 b4513 > jiang tianyi color does not have, beautiful glistening white air solitary worldly entanglements. < / font > < / u > < br > "+ / / increase font" < big > < font color = "# 800080" > river who first month? Jiang Yue he early according to people? </font></big><br>" + "</span>") return HtmlCompat.fromHtml(htmlStr2, HtmlCompat.FROM_HTML_SEPARATOR_LINE_BREAK_PARAGRAPH) }Copy the code

Run as follows

2.SpannableString

SpannableString can be used to add various display styles to a string using the setSpan method, and it is very easy to specify the size of the interval set. If you need to change it multiple times during use, use SpannableStringBuilder. Public void setSpan(Object what, int start, int end, int flags) public void setSpan(Object what, int start, int end, int flags) Flag Does not contain subscripts and has the following values:

  • int SPAN_EXCLUSIVE_EXCLUSIVE = 33;Start and end are not included
  • int SPAN_EXCLUSIVE_INCLUSIVE = 34;Does not contain start, contains end
  • int SPAN_INCLUSIVE_EXCLUSIVE = 17;Contains start, not end
  • int SPAN_INCLUSIVE_INCLUSIVE = 18;Contains start and end

The common spans are as follows

ForegroundColorSpan

ForegroundColorSpan is used to set the foreground color of the text

Val STR = """ Yan yan with thousands of miles, where spring without moon! The river flows around fangdian, as on the flower forest are like graupel; Empty flow frost do not feel fly, white sand on the tin can not see. Jiangtian color without fine dust, bright sky solitary moon wheel. Riverside who first saw the moon? Jiang Yue he early according to people? """.trimIndent() val ssb = SpannableStringBuilder(str) val colors = arrayListOf("#90EE90", "#FF00FF", "#3CB371", "#FF4500", "#8B4513", "#800080") for (index in 0 until 6) { ssb.setSpan( ForegroundColorSpan( Color.parseColor(colors[index])), index*17, (index+1)*17, SPAN_EXCLUSIVE_EXCLUSIVE ) }Copy the code

BackgroundColorSpan

BackgroundColorSpan can be used to set the background color

ssb.setSpan(BackgroundColorSpan(Color.parseColor("#808080")), 0, 17, SPAN_EXCLUSIVE_EXCLUSIVE)
Copy the code

AbsoluteSizeSpan

AbsoluteSizeSpan sets the text size

ssb.setSpan(AbsoluteSizeSpan(60), 17, 34, SPAN_EXCLUSIVE_EXCLUSIVE)
Copy the code

StrikethroughSpan

StrikethroughSpan is used to set StrikethroughSpan

ssb.setSpan(StrikethroughSpan(), 34, 51, SPAN_EXCLUSIVE_EXCLUSIVE)
Copy the code

UnderlineSpan

UnderlineSpan is used to set the underline

ssb.setSpan(UnderlineSpan(), 51, 68, SPAN_EXCLUSIVE_EXCLUSIVE)
Copy the code

ImageSpan

ImageSpan is used to replace images

/ / will be replaced with the moon in the poem image val drawable. = ContextCompat getDrawable (this, R.m ipmap. Moon) drawable!! .setBounds(0, 0, drawable.intrinsicWidth, drawable.intrinsicHeight) val span = ImageSpan(drawable, ImageSpan. ALIGN_BASELINE), SSB. SetSpan (span, 81, SPAN_EXCLUSIVE_EXCLUSIVE)Copy the code

ClickableSpan

ClickableSpan can add click events. When using ClickableSpan, you must set movementMethod for the corresponding TextView

textView.movementMethod = LinkMovementMethod.getInstance() ssb.setSpan(object :ClickableSpan(){ override fun onClick(p0: View) {toast.maketext (this@SpanActivity," click event ", toast.length_long).show()}}, 85, 101, SPAN_EXCLUSIVE_EXCLUSIVE)Copy the code

The renderings of the above Settings are as follows