Skip to content

Commit ebf1641

Browse files
committed
* fix click / + append
1 parent 235a73e commit ebf1641

File tree

5 files changed

+79
-14
lines changed

5 files changed

+79
-14
lines changed

span/src/main/java/com/angcyo/widget/span/DslDrawableSpan.kt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import android.view.View
1212
import androidx.annotation.ColorInt
1313
import androidx.annotation.Px
1414
import androidx.core.view.GravityCompat
15+
import kotlin.math.max
1516
import kotlin.math.min
1617

1718
/**
@@ -52,6 +53,9 @@ open class DslDrawableSpan : ReplacementSpan(), IWeightSpan, IClickableSpan, IDr
5253
/**限制span的最大宽度*/
5354
var spanMaxWidth: Int = undefined_int
5455

56+
/**限制span的最小宽度*/
57+
var spanMinWidth: Int = undefined_int
58+
5559
/**span相对于[TextView]的比例, 不支持平分. 需要[DslSpanTextView]支持*/
5660
var spanWeight: Float = undefined_float
5761

@@ -101,7 +105,7 @@ open class DslDrawableSpan : ReplacementSpan(), IWeightSpan, IClickableSpan, IDr
101105
var gradientStrokeColor = undefined_color
102106

103107
/**边框的宽度*/
104-
var gradientStrokeWidth = 1 * dp
108+
var gradientStrokeWidth = 0.5f * dp
105109

106110
/**圆角大小*/
107111
var gradientRadius = 25 * dp
@@ -128,7 +132,8 @@ open class DslDrawableSpan : ReplacementSpan(), IWeightSpan, IClickableSpan, IDr
128132

129133
val _gradientRectF = RectF()
130134

131-
/**单击事件回调, 需要[SpanClickMethod]支持*/
135+
/**单击事件回调, 需要[SpanClickMethod]支持
136+
* [com.angcyo.widget.span.SpanClickMethod.Companion.install]*/
132137
var spanClickAction: ((view: View, span: DslDrawableSpan) -> Unit)? = null
133138

134139
fun _initPaint(paint: Paint) {
@@ -173,7 +178,7 @@ open class DslDrawableSpan : ReplacementSpan(), IWeightSpan, IClickableSpan, IDr
173178

174179
val textWidth = textPaint.measureText(targetText, 0, targetText.length).toInt()
175180

176-
val bgWidth = _drawableWidth(backgroundDrawable)
181+
val bgWidth = max(spanMinWidth, _drawableWidth(backgroundDrawable))
177182
val fgWidth = _drawableWidth(foregroundDrawable)
178183

179184
val bgHeight = _drawableHeight(backgroundDrawable)

span/src/main/java/com/angcyo/widget/span/DslSpan.kt

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import android.text.SpannableStringBuilder
77
import android.text.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
88
import android.text.style.*
99
import android.view.Gravity
10+
import android.view.View
11+
import android.widget.TextView
1012
import androidx.annotation.ColorInt
1113
import androidx.annotation.Px
1214
import kotlin.math.max
@@ -18,7 +20,7 @@ import kotlin.math.max
1820
* @date 2020/01/08
1921
*/
2022

21-
class DslSpan {
23+
class DslSpan : Appendable {
2224

2325
val _builder = SpannableStringBuilder()
2426

@@ -167,7 +169,7 @@ class DslSpan {
167169
}
168170

169171
/**追加空隙*/
170-
fun appendSpace(@Px size: Int, @ColorInt color: Int = Color.TRANSPARENT): DslSpan {
172+
fun appendSpace(@Px size: Int = 10 * dpi, @ColorInt color: Int = Color.TRANSPARENT): DslSpan {
171173
append("<space>", SpaceSpan(size, color))
172174
return this
173175
}
@@ -208,12 +210,45 @@ class DslSpan {
208210
return this
209211
}
210212

213+
/**快速追加一个可以点击的文本*/
214+
fun click(
215+
textView: TextView?,
216+
text: CharSequence? = null,
217+
textColor: Int = "#4FB4F9".toColorInt(),
218+
action: DslDrawableSpan.() -> Unit = {},
219+
clickAction: (view: View, span: DslDrawableSpan) -> Unit
220+
): DslSpan {
221+
append("<click>", DslDrawableSpan().apply {
222+
SpanClickMethod.install(textView)
223+
showText = text
224+
this.textColor = textColor
225+
spanClickAction = clickAction
226+
this.action()
227+
})
228+
return this
229+
}
230+
211231
/**快速追加[DslTextSpan]*/
212232
fun text(text: CharSequence?, action: DslTextSpan.() -> Unit = {}): DslSpan {
213233
append(text, DslTextSpan().apply(action))
214234
return this
215235
}
216236

237+
override fun append(csq: CharSequence?): Appendable {
238+
append(text = csq)
239+
return this
240+
}
241+
242+
override fun append(csq: CharSequence?, start: Int, end: Int): Appendable {
243+
append(text = csq?.subSequence(start, end))
244+
return this
245+
}
246+
247+
override fun append(c: Char): Appendable {
248+
append(text = c.toString())
249+
return this
250+
}
251+
217252
override fun toString(): String {
218253
return _builder.toString()
219254
}
@@ -276,12 +311,15 @@ data class DslSpanConfig(
276311
fun DslSpan.drawableTipBorder(
277312
text: CharSequence? = null,
278313
borderColor: Int = "#0EC300".toColorInt(),
314+
solidColor: Int = Color.WHITE,
315+
radius: Float = 10 * dp,
316+
textSize: Float = 10 * dp,
279317
action: DslDrawableSpan.() -> Unit = {}
280318
): DslSpan {
281319
return drawable(text) {
282-
gradientSolidColor = Color.WHITE
283-
gradientRadius = 10 * dp
284-
textSize = 9 * dp
320+
gradientSolidColor = solidColor
321+
gradientRadius = radius
322+
this.textSize = textSize
285323
textGravity = Gravity.CENTER
286324
textColor = borderColor
287325
gradientStrokeColor = textColor
@@ -295,14 +333,18 @@ fun DslSpan.drawableTipBorder(
295333
fun DslSpan.drawableTipFill(
296334
text: CharSequence? = null,
297335
solidColor: Int = Color.RED,
336+
textColor: Int = Color.WHITE,
337+
radius: Float = 10 * dp,
338+
textSize: Float = 10 * dp,
298339
action: DslDrawableSpan.() -> Unit = {}
299340
): DslSpan {
300341
return drawable(text) {
301342
gradientSolidColor = solidColor
302-
gradientRadius = 10 * dp
343+
gradientRadius = radius
344+
this.textSize = textSize
303345
paddingHorizontal(4 * dpi)
304346
paddingVertical(2 * dpi)
305-
textColor = Color.WHITE
347+
this.textColor = textColor
306348
action()
307349
}
308350
}

span/src/main/java/com/angcyo/widget/span/DslTextSpan.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ open class DslTextSpan : MetricAffectingSpan(), LeadingMarginSpan,
5151
/**x轴缩放*/
5252
var scaleX: Float = undefined_float
5353

54-
/**文本基线偏移*/
54+
/**文本基线偏移, 可以实现[isSuperscript] [isSubscript] 上下标的效果*/
5555
var textBaselineShift: Int = 0
5656

5757
/**上标*/
@@ -77,6 +77,8 @@ open class DslTextSpan : MetricAffectingSpan(), LeadingMarginSpan,
7777
var onClickSpan: ((view: View, span: DslTextSpan) -> Unit)? = null
7878

7979
override fun updateDrawState(textPaint: TextPaint) {
80+
81+
//
8082
if (textColor != undefined_color) {
8183
textPaint.color = textColor
8284
}
@@ -90,7 +92,7 @@ open class DslTextSpan : MetricAffectingSpan(), LeadingMarginSpan,
9092
textPaint.textSize = relativeSizeScale * textPaint.textSize
9193
}
9294

93-
95+
//
9496
if (deleteLine) {
9597
textPaint.isStrikeThruText = true
9698
}
@@ -107,6 +109,7 @@ open class DslTextSpan : MetricAffectingSpan(), LeadingMarginSpan,
107109
textPaint.textScaleX = textPaint.textScaleX * scaleX
108110
}
109111

112+
//
110113
if (isSuperscript) {
111114
textPaint.baselineShift += (textPaint.ascent() / 2).toInt()
112115
}

span/src/main/java/com/angcyo/widget/span/SpanClickMethod.kt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,19 @@ class SpanClickMethod : LinkMovementMethod() {
3232
/**安装在[TextView]中
3333
* [LinkMovementMethod.getInstance()]*/
3434
fun install(textView: TextView?) {
35-
textView?.movementMethod = instance
35+
textView?.apply {
36+
if (movementMethod != instance) {
37+
movementMethod = instance
38+
}
39+
}
40+
}
41+
42+
fun uninstall(textView: TextView?) {
43+
textView?.apply {
44+
if (movementMethod == instance) {
45+
movementMethod = null
46+
}
47+
}
3648
}
3749
}
3850

span/src/main/java/com/angcyo/widget/text/DslSpanTextView.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ open class DslSpanTextView : AppCompatTextView {
7575
span.setDrawableColor(color)
7676
}
7777
}
78+
invalidate()
7879
}
7980

8081
/**添加额外的状态*/
@@ -101,7 +102,9 @@ open class DslSpanTextView : AppCompatTextView {
101102
//设置内置span的weight支持
102103
spans { _, span ->
103104
if (span is IWeightSpan) {
104-
span.onMeasure(widthSize, heightSize)
105+
val width = widthSize - paddingLeft - paddingRight
106+
val height = heightSize - paddingTop - paddingBottom
107+
span.onMeasure(width, height)
105108
}
106109
}
107110
}

0 commit comments

Comments
 (0)