RTL适配:送礼弹窗、礼物飘窗

This commit is contained in:
max
2024-04-12 09:38:43 +08:00
parent c5d0b80f1b
commit a39a11343c
7 changed files with 136 additions and 72 deletions

View File

@@ -65,63 +65,17 @@ class SpannableTextBuilder(private val textView: TextView) {
val start = spannableBuilder.length
spannableBuilder.append(text)
val end = spannableBuilder.length
// 文本颜色
if (textColor != null) {
spannableBuilder.setSpan(
ForegroundColorSpan(textColor),
start,
end,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
)
}
// 文本背景颜色
if (backgroundColor != null) {
spannableBuilder.setSpan(
BackgroundColorSpan(backgroundColor),
start,
end,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
)
}
// 文本大小
if (textSize != null) {
spannableBuilder.setSpan(
AbsoluteSizeSpan(textSize, true),
start,
end,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
)
}
// 文本样式
if (textStyle != null) {
spannableBuilder.setSpan(
StyleSpan(textStyle),
start,
end,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
)
}
// 下划线
if (underline == true) {
spannableBuilder.setSpan(UnderlineSpan(), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
}
// 点击事件
if (clickListener != null) {
// 设置highlightColor=Color.TRANSPARENT可以解决点击时的高亮色问题但光标的区域选中也是透明的貌似对用户体验不太好
// textView.highlightColor = Color.TRANSPARENT
textView.movementMethod = LinkMovementMethod.getInstance()
val clickableSpan = TextClickableSpan(
clickListener, text, textColor
?: textView.currentTextColor, underline ?: false
)
spannableBuilder.setSpan(clickableSpan, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
}
setTextStyle(
text,
start,
end,
textColor,
backgroundColor,
textSize,
textStyle,
underline,
clickListener
)
return this
}
@@ -224,6 +178,109 @@ class SpannableTextBuilder(private val textView: TextView) {
return this
}
private fun setTextStyle(
text: String,
start: Int,
end: Int,
@ColorInt textColor: Int? = null,
@ColorInt backgroundColor: Int? = null,
textSize: Int? = null,
textStyle: Int? = null,
underline: Boolean? = null,
clickListener: ((String) -> Unit)? = null
): SpannableTextBuilder {
if (start < 0 || end > spannableBuilder.length) {
return this
}
// 文本颜色
if (textColor != null) {
spannableBuilder.setSpan(
ForegroundColorSpan(textColor),
start,
end,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
)
}
// 文本背景颜色
if (backgroundColor != null) {
spannableBuilder.setSpan(
BackgroundColorSpan(backgroundColor),
start,
end,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
)
}
// 文本大小
if (textSize != null) {
spannableBuilder.setSpan(
AbsoluteSizeSpan(textSize, true),
start,
end,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
)
}
// 文本样式
if (textStyle != null) {
spannableBuilder.setSpan(
StyleSpan(textStyle),
start,
end,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
)
}
// 下划线
if (underline == true) {
spannableBuilder.setSpan(UnderlineSpan(), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
}
// 点击事件
if (clickListener != null) {
// 设置highlightColor=Color.TRANSPARENT可以解决点击时的高亮色问题但光标的区域选中也是透明的貌似对用户体验不太好
// textView.highlightColor = Color.TRANSPARENT
textView.movementMethod = LinkMovementMethod.getInstance()
val clickableSpan = TextClickableSpan(
clickListener, text, textColor
?: textView.currentTextColor, underline ?: false
)
spannableBuilder.setSpan(clickableSpan, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
}
return this
}
fun setTextStyle(
text: String,
@ColorInt textColor: Int? = null,
@ColorInt backgroundColor: Int? = null,
textSize: Int? = null,
textStyle: Int? = null,
underline: Boolean? = null,
clickListener: ((String) -> Unit)? = null
): SpannableTextBuilder {
if (text.isEmpty()) {
return this
}
val start = spannableBuilder.indexOf(text)
if (start == -1) {
return this
}
val end = start + text.length
return setTextStyle(
text,
start,
end,
textColor,
backgroundColor,
textSize,
textStyle,
underline,
clickListener
)
}
fun build(): SpannableStringBuilder {
return spannableBuilder
}