Marketing frameworks of the same type simply use chained programming to simplify method calls and limit official methods

But rich text is basically a placeholder or concatenated string, so the expectation is to concatenate Span and regular replacements just like strings

Here’s a recommendation for Spannable’s extension library: Spannable

Implement effect preview

The CharSequence interface is used for all general functions, and is used no differently than a string

Span can be set/inserted/added

// Display prices by stitching
tv5.text = "Selections".setSpan(ColorSpan("#ed6a2c"))
  .addSpan("39.9", arrayOf(ColorSpan("#ed6a2c"), AbsoluteSizeSpan(18.true)))
  .addSpan("1000+ persons for payment")
  .addSpan("image", CenterImageSpan(this, R.drawable.ic_touch).setDrawableSize(20, dp = true))
Copy the code

You can create styles using literal replacement/re matches just like strings

tv.movementMethod = ClickableMovementMethod.getInstance()
tv.text = "Privacy policy | | license brand guidelines".replaceSpan(Privacy Policy) {
  URLSpan("https://github.com/") // Only replace effects
}

tv.text = "Privacy policy | | license brand guidelines".replaceSpan(Privacy Policy) {
  listOf(URLSpan("https://github.com/"), ColorSpan("#ed6a2c")) // Replace multiple effects
}

tv.text = "Privacy policy | | license brand guidelines".replaceSpan(Privacy Policy) {
		SpannableString("Friendship link").setSpan(URLSpan("https://github.com/")) // Replace the text and Span effect
}

// Regular substitution can also be used
tv2.text = "We can create a user @username or create a # trending hashtag."
.replaceSpan("@ [^ @] +? (? =\\s|\$)".toRegex()) { matchResult ->
      HighlightSpan("#ed6a2c") {
          Toast.makeText(this@MainActivity."Click on user${matchResult.value}", Toast.LENGTH_SHORT).show()
      }
   }

// You can quickly render placeholder rich text with rich re/match features
tv2.text = "The current monetary unit is {{US $}}".replaceSpan("{${}}"){
  CenterImageSpan(context, R.drawable.ic_dollar) // Replace the placeholder map with an image
}
Copy the code

Since all function return values and receivers are CharSequence, you can even encapsulate a set of Span rules into a function, such as implementing live broadcast messages/comment content/simple rich text input fields

function

Use very simple functions

function introduce
setSpan Sets the Span
addSpan Add/insert Span or string
replaceSpan Replace/re replaces a Span or string
replaceSpanFirst/replaceSpanLast Replace/re Replaces the Span or string of the first/last match

I find Span to be extremely unscalable and add a bunch of functions to save a few words to the cost of learning. If you prefer DSL or chained programming, you can use another author’s forked version :SpannableX

And includes some commonly used Span effect implementations, welcome to contribute your own

Span describe
CenterImageSpan Vertical alignment/image width and height/fixed image ratio
GlideImageSpan Load network image/vertical alignment/image width/height/fixed image ratio
ColorSpan Create text colors quickly
HighlightSpan Create font color/font style/clickable effect
ClickableMovementMethod Equivalent LinkMovementMethod, but without clicking on the background color

Only ImageSpan is officially provided, and vertical alignment is required above API23, and the width cannot be specified so two new Imagespans are provided

CenterImageSpan supports image width and height specification, and zooming does not cause the image to stretch. Vertical center alignment, adaptive height

GlideImageSpan supports downloading images and using Glide for more custom image loading options (such as cropping/rounding/placeholder)