@@ -28,6 +28,8 @@ import android.text.style.*
2828import android.view.View
2929import androidx.annotation.ColorInt
3030import androidx.annotation.DrawableRes
31+ import androidx.annotation.FloatRange
32+ import androidx.annotation.IntRange
3133import androidx.core.text.inSpans
3234
3335private const val IMAGE_SPAN_TEXT = " <img/>"
@@ -92,18 +94,14 @@ inline fun SpannableStringBuilder.bullet(
9294 gapWidth : Int = BulletSpan .STANDARD_GAP_WIDTH ,
9395 @ColorInt color : Int? = null,
9496 builderAction : SpannableStringBuilder .() -> Unit
95- ): SpannableStringBuilder {
96- val span = if (color == null ) BulletSpan (gapWidth) else BulletSpan (gapWidth, color)
97- return inSpans(span, builderAction)
98- }
97+ ): SpannableStringBuilder =
98+ inSpans(if (color == null ) BulletSpan (gapWidth) else BulletSpan (gapWidth, color), builderAction)
9999
100100inline fun SpannableStringBuilder.quote (
101101 @ColorInt color : Int? = null,
102102 builderAction : SpannableStringBuilder .() -> Unit
103- ): SpannableStringBuilder {
104- val span = if (color == null ) QuoteSpan () else QuoteSpan (color)
105- return inSpans(span, builderAction)
106- }
103+ ): SpannableStringBuilder =
104+ inSpans(if (color == null ) QuoteSpan () else QuoteSpan (color), builderAction)
107105
108106inline fun SpannableStringBuilder.leadingMargin (
109107 first : Float ,
@@ -117,22 +115,6 @@ inline fun SpannableStringBuilder.leadingMargin(
117115 builderAction : SpannableStringBuilder .() -> Unit
118116): SpannableStringBuilder = inSpans(LeadingMarginSpan .Standard (first, rest), builderAction)
119117
120- fun SpannableStringBuilder.clickable (
121- @ColorInt color : Int? = null,
122- isUnderlineText : Boolean = true,
123- onClick : (View ) -> Unit ,
124- builderAction : SpannableStringBuilder .() -> Unit
125- ): SpannableStringBuilder = inSpans(object : ClickableSpan () {
126- override fun onClick (widget : View ) {
127- onClick(widget)
128- }
129-
130- override fun updateDrawState (ds : TextPaint ) {
131- ds.color = color ? : ds.linkColor
132- ds.isUnderlineText = isUnderlineText
133- }
134- }, builderAction)
135-
136118fun SpannableStringBuilder.append (
137119 drawable : Drawable ,
138120 width : Int = drawable.intrinsicWidth,
@@ -157,37 +139,50 @@ fun SpannableStringBuilder.appendClickable(
157139 @ColorInt color : Int? = null,
158140 isUnderlineText : Boolean = true,
159141 onClick : (View ) -> Unit
160- ): SpannableStringBuilder = clickable( color, isUnderlineText, onClick) { append(text) }
142+ ): SpannableStringBuilder = inSpans( ClickableSpan ( color, isUnderlineText, onClick) ) { append(text) }
161143
162144fun SpannableStringBuilder.appendClickable (
163145 drawable : Drawable ,
164146 width : Int = drawable.intrinsicWidth,
165147 height : Int = drawable.intrinsicHeight,
166148 onClick : (View ) -> Unit
167- ): SpannableStringBuilder = clickable( onClick = onClick) { append(drawable, width, height) }
149+ ): SpannableStringBuilder = inSpans( ClickableSpan ( onClick = onClick) ) { append(drawable, width, height) }
168150
169151fun SpannableStringBuilder.appendClickable (
170152 @DrawableRes resourceId : Int ,
171153 context : Context = application,
172154 onClick : (View ) -> Unit
173- ): SpannableStringBuilder = clickable( onClick = onClick) { append(resourceId, context) }
155+ ): SpannableStringBuilder = inSpans( ClickableSpan ( onClick = onClick) ) { append(resourceId, context) }
174156
175157fun SpannableStringBuilder.appendClickable (
176158 bitmap : Bitmap ,
177159 context : Context = application,
178160 onClick : (View ) -> Unit
179- ): SpannableStringBuilder = clickable( onClick = onClick) { append(bitmap, context) }
161+ ): SpannableStringBuilder = inSpans( ClickableSpan ( onClick = onClick) ) { append(bitmap, context) }
180162
181163fun SpannableStringBuilder.appendSpace (
182- @androidx.annotation. FloatRange (from = 0.0) size : Float ,
164+ @FloatRange(from = 0.0 ) size : Float ,
183165 @ColorInt color : Int = Color .TRANSPARENT
184166): SpannableStringBuilder = appendSpace(size.toInt(), color)
185167
186168fun SpannableStringBuilder.appendSpace (
187- @androidx.annotation. IntRange (from = 0) size : Int ,
169+ @IntRange(from = 0 ) size : Int ,
188170 @ColorInt color : Int = Color .TRANSPARENT
189171): SpannableStringBuilder = inSpans(SpaceSpan (size, color)) { append(SPACE_SPAN_TEXT ) }
190172
173+ fun ClickableSpan (
174+ @ColorInt color : Int? = null,
175+ isUnderlineText : Boolean = true,
176+ onClick : (View ) -> Unit ,
177+ ): ClickableSpan = object : ClickableSpan () {
178+ override fun onClick (widget : View ) = onClick(widget)
179+
180+ override fun updateDrawState (ds : TextPaint ) {
181+ ds.color = color ? : ds.linkColor
182+ ds.isUnderlineText = isUnderlineText
183+ }
184+ }
185+
191186class SpaceSpan constructor(private val width : Int , color : Int = Color .TRANSPARENT ) : ReplacementSpan() {
192187 private val paint = Paint ().apply {
193188 this .color = color
@@ -196,18 +191,17 @@ class SpaceSpan constructor(private val width: Int, color: Int = Color.TRANSPARE
196191
197192 override fun getSize (
198193 paint : Paint , text : CharSequence? ,
199- @androidx.annotation. IntRange (from = 0) start : Int ,
200- @androidx.annotation. IntRange (from = 0) end : Int ,
194+ @IntRange(from = 0 ) start : Int ,
195+ @IntRange(from = 0 ) end : Int ,
201196 fm : Paint .FontMetricsInt ?
202197 ): Int = width
203198
204199 override fun draw (
205200 canvas : Canvas , text : CharSequence? ,
206- @androidx.annotation. IntRange (from = 0) start : Int ,
207- @androidx.annotation. IntRange (from = 0) end : Int ,
201+ @IntRange(from = 0 ) start : Int ,
202+ @IntRange(from = 0 ) end : Int ,
208203 x : Float , top : Int , y : Int , bottom : Int , paint : Paint
209- ) =
210- canvas.drawRect(x, top.toFloat(), x + width, bottom.toFloat(), this .paint)
204+ ) = canvas.drawRect(x, top.toFloat(), x + width, bottom.toFloat(), this .paint)
211205}
212206
213207class TypefaceSpanCompat (private val newType : Typeface ) : TypefaceSpan(null ) {
0 commit comments