Merge pull request #242 from TouchInstinct/view/styleable_text

View/styleable text
This commit is contained in:
Kirill Nayduik 2022-01-26 17:10:56 +03:00 committed by GitHub
commit cbd94750bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 28 deletions

View File

@ -76,7 +76,6 @@ fun CharSequence.toClickableSubstringText(
}
override fun updateDrawState(ds: TextPaint) {
super.updateDrawState(ds)
ds.isUnderlineText = isUnderlineText
}
}
@ -92,7 +91,9 @@ fun CharSequence.toStyleableSubstringText(
private fun CharSequence.toSubstringSpannable(
substring: String,
span: Any?
) = SpannableString(this)
) = toSpannable()
.apply {
indexesOf(substring)?.let { (startSpan, endSpan) -> setSpan(span, startSpan, endSpan, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) }
}
private fun CharSequence.toSpannable() = if (this is SpannableString) this else SpannableString(this)

View File

@ -2,18 +2,13 @@ package ru.touchin.roboswag.views.text_view
import android.content.Context
import android.graphics.Color
import android.text.Spanned
import android.text.TextPaint
import android.text.style.ClickableSpan
import android.text.style.TextAppearanceSpan
import android.util.AttributeSet
import android.view.View
import androidx.annotation.ColorInt
import androidx.annotation.StyleRes
import androidx.core.content.withStyledAttributes
import androidx.core.text.toSpannable
import ru.touchin.extensions.getResourceIdOrNull
import ru.touchin.extensions.indexesOf
import ru.touchin.roboswag.components.utils.movementmethods.ClickableMovementMethod
import ru.touchin.roboswag.components.utils.spans.toClickableSubstringText
import ru.touchin.roboswag.views.R
/**
@ -28,7 +23,7 @@ class MultipleActionTextView @JvmOverloads constructor(
private val onClickActions = mutableListOf<SubstringClickAction>()
@ColorInt
@StyleRes
private var textStyle: Int? = null
private var isUnderlineText = false
@ -52,24 +47,7 @@ class MultipleActionTextView @JvmOverloads constructor(
private fun applyActionSpans() {
text = text.toSpannable().apply {
onClickActions.forEach { (substring, action) ->
indexesOf(substring)?.let { (startSpan, endSpan) ->
setSpan(object : ClickableSpan() {
override fun onClick(widget: View) {
action.invoke()
}
override fun updateDrawState(ds: TextPaint) {
super.updateDrawState(ds)
ds.isUnderlineText = isUnderlineText
}
}, startSpan, endSpan, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
textStyle?.let { setSpan(TextAppearanceSpan(context, it), startSpan, endSpan, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) }
}
}
onClickActions.forEach { (substring, action) -> toClickableSubstringText(substring, action, isUnderlineText, textStyle, context) }
}
}

View File

@ -0,0 +1,35 @@
package ru.touchin.roboswag.views.text_view
import android.content.Context
import android.util.AttributeSet
import androidx.core.content.withStyledAttributes
import ru.touchin.extensions.getResourceIdOrNull
import ru.touchin.roboswag.components.utils.spans.toStyleableSubstringText
import ru.touchin.roboswag.views.R
/**
* A [android.widget.TextView] which support styling substrings of text
*/
class StyleableSubTextView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
): EllipsizeSpannableTextView(context, attrs, defStyleAttr) {
init {
context.withStyledAttributes(attrs, R.styleable.StyleableSubTextView, defStyleAttr, 0) {
val substring = getString(R.styleable.StyleableSubTextView_subtext)
val subtextStyle = getResourceIdOrNull(R.styleable.StyleableSubTextView_subtextStyle)
if (subtextStyle != null && substring != null) {
text = text.toStyleableSubstringText(
substring = substring,
styleId = subtextStyle,
context = context
)
}
}
}
}