Action TextViews bugfix

This commit is contained in:
Kirill Nayduik 2022-01-26 16:58:24 +03:00
parent ee4c4aaa5a
commit 3d01b780be
2 changed files with 7 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) }
}
}