@@ -22,6 +22,7 @@ import android.util.AttributeSet
2222import android.util.LayoutDirection
2323import android.view.View
2424import androidx.annotation.StringRes
25+ import androidx.core.content.ContextCompat
2526import androidx.core.text.TextUtilsCompat
2627import java.util.Locale
2728
@@ -49,11 +50,35 @@ class FormatTextView : BaseTextView {
4950 defStyleAttr
5051 )
5152
52- fun setFormatTextBean (@StringRes formatTextRes : Int , vararg args : BaseFormat ? ) {
53- setFormatTextBean(resources.getString(formatTextRes), * args)
53+ fun setFormatText (@StringRes formatTextRes : Int , vararg args : Int ) {
54+ setFormatText(resources.getString(formatTextRes), * args)
55+ }
56+
57+ fun setFormatText (formatTextValue : String? , vararg args : Int ) {
58+ val formatTexts: Array <FormatText ?> = arrayOfNulls(args.size)
59+ for (i in args.indices) {
60+ formatTexts[i] = FormatText ().setResValue(args[i])
61+ }
62+ setFormatText(formatTextValue, * formatTexts)
63+ }
64+
65+ fun setFormatText (@StringRes formatTextRes : Int , vararg args : String ) {
66+ setFormatText(resources.getString(formatTextRes), * args)
5467 }
5568
56- fun setFormatTextBean (formatTextValue : String? , vararg args : BaseFormat ? ) {
69+ fun setFormatText (formatTextValue : String? , vararg args : String ) {
70+ val formatTexts: Array <FormatText ?> = arrayOfNulls(args.size)
71+ for (i in args.indices) {
72+ formatTexts[i] = FormatText ().setStrValue(args[i])
73+ }
74+ setFormatText(formatTextValue, * formatTexts)
75+ }
76+
77+ fun setFormatText (@StringRes formatTextRes : Int , vararg args : BaseFormat ? ) {
78+ setFormatText(resources.getString(formatTextRes), * args)
79+ }
80+
81+ fun setFormatText (formatTextValue : String? , vararg args : BaseFormat ? ) {
5782 var textValue = formatTextValue?.replace(" \\ r\\ n" .toRegex(), " <br>" )
5883 textValue = textValue?.replace(" \\ n" .toRegex(), " <br>" )
5984 textValue = textValue?.replace(" \\ r" .toRegex(), " <br>" )
@@ -122,31 +147,6 @@ class FormatTextView : BaseTextView {
122147 text = richText?.let { formatArgs?.let { it1 -> getCustomStyleHtml(it, * it1) } }
123148 }
124149
125- fun setFormatText (@StringRes formatTextRes : Int , vararg args : Int ) {
126- setFormatText(resources.getString(formatTextRes), * args)
127- }
128-
129- fun setFormatText (formatTextValue : String? , vararg args : Int ) {
130- val formatTexts: Array <FormatText ?> = arrayOfNulls(args.size)
131- for (i in args.indices) {
132- formatTexts[i] = FormatText ().setResValue(args[i])
133- }
134- setFormatTextBean(formatTextValue, * formatTexts)
135- }
136-
137- fun setFormatText (@StringRes formatTextRes : Int , vararg args : String ) {
138- setFormatText(resources.getString(formatTextRes), * args)
139- }
140-
141- fun setFormatText (formatTextValue : String? , vararg args : String ) {
142- val formatTexts: Array <FormatText ?> = arrayOfNulls(args.size)
143- for (i in args.indices) {
144- formatTexts[i] = FormatText ().setStrValue(args[i])
145- }
146- setFormatTextBean(formatTextValue, * formatTexts)
147- }
148-
149-
150150 private fun getCustomStyleHtml (html : String , vararg args : BaseFormat ? ): CharSequence? {
151151 val spannedHtml = Html .fromHtml(html)
152152 val htmlBuilder = SpannableStringBuilder (spannedHtml)
@@ -178,37 +178,39 @@ class FormatTextView : BaseTextView {
178178 } else {
179179 false
180180 }
181- val imageWidth = dp2px(context, formatImage.width)
182- val imageHeight = dp2px(context, formatImage.height)
181+ val imageWidth = formatImage.width.dp
182+ val imageHeight = formatImage.height.dp
183183 var marginLeft = if (isRtl) formatImage.marginEnd else formatImage.marginStart
184184 var marginRight = if (isRtl) formatImage.marginStart else formatImage.marginEnd
185185 marginLeft = if (marginLeft == 0f ){
186- dp2px(context, formatImage.marginLeft)
186+ formatImage.marginLeft.dp
187187 }else {
188- dp2px(context, marginLeft)
188+ marginLeft.dp
189189 }
190190
191191 marginRight = if (marginRight == 0f ){
192- dp2px(context, formatImage.marginRight)
192+ formatImage.marginRight.dp
193193 }else {
194- dp2px(context, marginRight)
194+ marginRight.dp
195195 }
196196
197197 val start = htmlBuilder.getSpanStart(urlSpan)
198198 val end = htmlBuilder.getSpanEnd(urlSpan)
199199 val flags = htmlBuilder.getSpanFlags(urlSpan)
200200 val clickableSpan: ClickableSpan = FormatClickableSpan (urlSpan)
201-
201+ htmlBuilder.removeSpan(formatImage)
202202 htmlBuilder.setSpan(clickableSpan, start, end, flags)
203203
204204 val drawable = LevelListDrawable ()
205205 if (formatImage.imageResValue != 0 ) {
206- val d = resources.getDrawable(formatImage.imageResValue)
207- val wh = getImageSpanWidthHeight(
208- if (imageWidth != 0f ) imageWidth else d.intrinsicWidth.toFloat(),
209- if (imageHeight != 0f ) imageHeight else d.intrinsicHeight.toFloat(),
210- d
211- )
206+ val d = ContextCompat .getDrawable(context,formatImage.imageResValue)
207+ val wh = d?.let {
208+ getImageSpanWidthHeight(
209+ if (imageWidth != 0f ) imageWidth else d.intrinsicWidth.toFloat(),
210+ if (imageHeight != 0f ) imageHeight else d.intrinsicHeight.toFloat(),
211+ d
212+ )
213+ }
212214 val insetDrawable =
213215 InsetDrawable (
214216 d,
@@ -220,20 +222,22 @@ class FormatTextView : BaseTextView {
220222 drawable.addLevel(1 , 1 , insetDrawable)
221223 drawable.setBounds(
222224 0 , 0 ,
223- wh[ 0 ] .toInt() + marginLeft.toInt() + marginRight.toInt(),
224- wh[ 1 ] .toInt()
225+ (wh?.get( 0 )? .toInt() ? : 0 ) + marginLeft.toInt() + marginRight.toInt(),
226+ (wh?.get( 1 )?.toInt() ? : 0 ) .toInt()
225227 )
226228 drawable.level = 1
227229 val imageSpan = FormatImageSpan (drawable, formatImage.verticalAlignment)
228230 htmlBuilder.setSpan(imageSpan, start, end, flags)
229231 } else {
230232 if (formatImage.imagePlaceHolder != 0 ) {
231- val d = resources.getDrawable(formatImage.imagePlaceHolder)
232- val wh = getImageSpanWidthHeight(
233- if (imageWidth != 0f ) imageWidth else d.intrinsicWidth.toFloat(),
234- if (imageHeight != 0f ) imageHeight else d.intrinsicHeight.toFloat(),
235- d
236- )
233+ val d = ContextCompat .getDrawable(context,formatImage.imagePlaceHolder)
234+ val wh = d?.let {
235+ getImageSpanWidthHeight(
236+ if (imageWidth != 0f ) imageWidth else d.intrinsicWidth.toFloat(),
237+ if (imageHeight != 0f ) imageHeight else d.intrinsicHeight.toFloat(),
238+ d
239+ )
240+ }
237241 val insetDrawable =
238242 InsetDrawable (
239243 d,
@@ -245,15 +249,15 @@ class FormatTextView : BaseTextView {
245249 drawable.addLevel(1 , 1 , insetDrawable)
246250 drawable.setBounds(
247251 0 , 0 ,
248- wh[ 0 ] .toInt() + marginLeft.toInt() + marginRight.toInt(),
249- wh[ 1 ] .toInt()
252+ (wh?.get( 0 )? .toInt() ? : 0 ) + marginLeft.toInt() + marginRight.toInt(),
253+ (wh?.get( 1 )?.toInt() ? : 0 ) .toInt()
250254 )
251255 drawable.level = 1
252256 }
253257 val imageSpan = FormatImageSpan (drawable, formatImage.verticalAlignment)
254258 htmlBuilder.setSpan(imageSpan, start, end, flags)
255259 if (onInflateImageListener != null ) {
256- onInflateImageListener? .onInflate(
260+ onInflateImageListener!! .onInflate(
257261 formatImage,
258262 object : OnReturnDrawableListener {
259263 override fun onReturnDrawable (d : Drawable ) {
@@ -309,37 +313,31 @@ class FormatTextView : BaseTextView {
309313 }
310314 if (underline && (formatText.underlineColor != 0 || formatText.underlineMarginTop != 0f || formatText.underlineWidth != 0f )) {
311315 val textPaint = TextPaint ()
312- textPaint.textSize = if (textSize > 0 ) sp2px(context, textSize) else getTextSize()
316+ textPaint.textSize = if (textSize > 0 ) textSize.sp else getTextSize()
313317 val fm = textPaint.fontMetrics
314318
315319 val underLineText = LineText (
316320 start,
317321 end,
318322 if (formatText.underlineColor != 0 ) resources.getColor(formatText.underlineColor) else textColor,
319- dp2px(context, formatText.underlineMarginTop) + fm.descent / 3 ,
320- if (formatText.underlineWidth == 0f ) dp2px(context, 1f ) else dp2px(
321- context,
322- formatText.underlineWidth
323- )
323+ formatText.underlineMarginTop.dp + fm.descent / 3 ,
324+ if (formatText.underlineWidth == 0f ) 1f .dp else formatText.underlineWidth.dp
324325 )
325326 underLineTexts.add(underLineText)
326327 userDefaultUnder = false
327328 }
328329 var userDefaultDelete = true
329330 if (deleteLine && (formatText.deleteLineColor != 0 || formatText.deleteLineWidth != 0f )) {
330331 val textPaint = TextPaint ()
331- textPaint.textSize = if (textSize > 0 ) sp2px(context, textSize) else getTextSize()
332+ textPaint.textSize = if (textSize > 0 ) textSize.sp else getTextSize()
332333 val fm = textPaint.fontMetrics
333334
334335 val deleteLineText = LineText (
335336 start,
336337 end,
337338 if (formatText.deleteLineColor != 0 ) resources.getColor(formatText.deleteLineColor) else textColor,
338339 (fm.descent - fm.ascent) / 2 - fm.descent,
339- if (formatText.deleteLineWidth == 0f ) dp2px(context, 1f ) else dp2px(
340- context,
341- formatText.deleteLineWidth
342- )
340+ if (formatText.deleteLineWidth == 0f ) 1f .dp else formatText.deleteLineWidth.dp
343341 )
344342 deleteLineTexts.add(deleteLineText)
345343 userDefaultDelete = false
@@ -348,7 +346,7 @@ class FormatTextView : BaseTextView {
348346 // gradient
349347 if (! isDrawGradient && formatText.gradient != null ) {
350348 val textPaint = TextPaint ()
351- textPaint.textSize = if (textSize > 0 ) sp2px(context, textSize) else getTextSize()
349+ textPaint.textSize = if (textSize > 0 ) textSize.sp else getTextSize()
352350 val fm = textPaint.fontMetrics
353351
354352 val gradientText = LineText (
@@ -438,11 +436,12 @@ class FormatTextView : BaseTextView {
438436 }
439437 }
440438 }
439+ htmlBuilder.removeSpan(urlSpan)
441440 htmlBuilder.setSpan(clickableSpan, start, end, flags)
442441 if (textSize > 0 ) {
443442 htmlBuilder.setSpan(
444443 AbsoluteSizeSpan (
445- sp2px(context, textSize) .toInt(),
444+ textSize.sp .toInt(),
446445 false
447446 ), start, end, flags
448447 )
@@ -476,10 +475,11 @@ class FormatTextView : BaseTextView {
476475 }
477476 }
478477
479- open inner class FormatClickableSpan (private val urlSpan : URLSpan ) : ClickableSpan() {
478+ open inner class FormatClickableSpan (urlSpan : URLSpan ) : ClickableSpan() {
479+ private var position = urlSpan.url.toInt()
480+
480481 override fun onClick (widget : View ) {
481- val url = urlSpan.url
482- onFormatClickListener?.onLabelClick(url.toInt())
482+ onFormatClickListener?.onLabelClick(position)
483483 isClickSpanItem = true
484484 }
485485 }
0 commit comments