@@ -56,29 +56,6 @@ class FormatTextView : BaseTextView {
5656
5757 if (args[i] is FormatImage ) {
5858 val formatImage = args[i] as FormatImage
59- val isRtl =
60- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .KITKAT ) {
61- TextUtilsCompat .getLayoutDirectionFromLocale(Locale .getDefault()) == LayoutDirection .RTL
62- } else {
63- false
64- }
65- formatImage.width = Utils .dp2px(context, formatImage.width)
66- formatImage.height = Utils .dp2px(context, formatImage.height)
67- formatImage.marginLeft = Utils .dp2px(
68- context,
69- max(
70- if (isRtl) formatImage.marginEnd else formatImage.marginStart,
71- formatImage.marginLeft
72- )
73- )
74- formatImage.marginRight = Utils .dp2px(
75- context,
76- max(
77- if (isRtl) formatImage.marginStart else formatImage.marginEnd,
78- formatImage.marginRight
79- )
80- )
81-
8259 strings[i] =
8360 start + " <img src=\" " + (if (formatImage.imageResValue != 0 ) formatImage.imageResValue else formatImage.imageUrlValue) + " \" ></img>" + end
8461 } else {
@@ -154,35 +131,55 @@ class FormatTextView : BaseTextView {
154131 urlSpan : URLSpan ,
155132 formatImage : FormatImage
156133 ) {
134+ val isRtl =
135+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .KITKAT ) {
136+ TextUtilsCompat .getLayoutDirectionFromLocale(Locale .getDefault()) == LayoutDirection .RTL
137+ } else {
138+ false
139+ }
140+ val imageWidth = Utils .dp2px(context, formatImage.width)
141+ val imageHeight = Utils .dp2px(context, formatImage.height)
142+ var marginLeft = if (isRtl) formatImage.marginEnd else formatImage.marginStart
143+ var marginRight = if (isRtl) formatImage.marginStart else formatImage.marginEnd
144+ marginLeft = if (marginLeft == 0f ){
145+ Utils .dp2px(context, formatImage.marginLeft)
146+ }else {
147+ Utils .dp2px(context, marginLeft)
148+ }
149+
150+ marginRight = if (marginRight == 0f ){
151+ Utils .dp2px(context, formatImage.marginRight)
152+ }else {
153+ Utils .dp2px(context, marginRight)
154+ }
155+
157156 val start = htmlBuilder.getSpanStart(urlSpan)
158157 val end = htmlBuilder.getSpanEnd(urlSpan)
159158 val flags = htmlBuilder.getSpanFlags(urlSpan)
160- val clickableSpan: ClickableSpan = object : FormatClickableSpan (urlSpan) {
161-
162- }
159+ val clickableSpan: ClickableSpan = FormatClickableSpan (urlSpan)
163160
164161 htmlBuilder.setSpan(clickableSpan, start, end, flags)
165162
166163 val drawable = LevelListDrawable ()
167164 if (formatImage.imageResValue != 0 ) {
168165 val d = resources.getDrawable(formatImage.imageResValue)
169166 val wh = getImageSpanWidthHeight(
170- if (formatImage.width != 0f ) formatImage.width else d.intrinsicWidth.toFloat(),
171- if (formatImage.height != 0f ) formatImage.height else d.intrinsicHeight.toFloat(),
167+ if (imageWidth != 0f ) imageWidth else d.intrinsicWidth.toFloat(),
168+ if (imageHeight != 0f ) imageHeight else d.intrinsicHeight.toFloat(),
172169 d
173170 )
174171 val insetDrawable =
175172 InsetDrawable (
176173 d,
177- formatImage. marginLeft.toInt(),
174+ marginLeft.toInt(),
178175 0 ,
179- formatImage. marginRight.toInt(),
176+ marginRight.toInt(),
180177 0
181178 )
182179 drawable.addLevel(1 , 1 , insetDrawable)
183180 drawable.setBounds(
184181 0 , 0 ,
185- wh[0 ].toInt() + formatImage. marginLeft.toInt() + formatImage. marginRight.toInt(),
182+ wh[0 ].toInt() + marginLeft.toInt() + marginRight.toInt(),
186183 wh[1 ].toInt()
187184 )
188185 drawable.level = 1
@@ -192,22 +189,22 @@ class FormatTextView : BaseTextView {
192189 if (formatImage.imagePlaceHolder != 0 ) {
193190 val d = resources.getDrawable(formatImage.imagePlaceHolder)
194191 val wh = getImageSpanWidthHeight(
195- if (formatImage.width != 0f ) formatImage.width else d.intrinsicWidth.toFloat(),
196- if (formatImage.height != 0f ) formatImage.height else d.intrinsicHeight.toFloat(),
192+ if (imageWidth != 0f ) imageWidth else d.intrinsicWidth.toFloat(),
193+ if (imageHeight != 0f ) imageHeight else d.intrinsicHeight.toFloat(),
197194 d
198195 )
199196 val insetDrawable =
200197 InsetDrawable (
201198 d,
202- formatImage. marginLeft.toInt(),
199+ marginLeft.toInt(),
203200 0 ,
204- formatImage. marginRight.toInt(),
201+ marginRight.toInt(),
205202 0
206203 )
207204 drawable.addLevel(1 , 1 , insetDrawable)
208205 drawable.setBounds(
209206 0 , 0 ,
210- wh[0 ].toInt() + formatImage. marginLeft.toInt() + formatImage. marginRight.toInt(),
207+ wh[0 ].toInt() + marginLeft.toInt() + marginRight.toInt(),
211208 wh[1 ].toInt()
212209 )
213210 drawable.level = 1
@@ -221,20 +218,20 @@ class FormatTextView : BaseTextView {
221218 override fun onReturnDrawable (d : Drawable ) {
222219 val insetDrawable = InsetDrawable (
223220 d,
224- formatImage. marginLeft.toInt(),
221+ marginLeft.toInt(),
225222 0 ,
226- formatImage. marginRight.toInt(),
223+ marginRight.toInt(),
227224 0
228225 )
229226 val wh = getImageSpanWidthHeight(
230- if (formatImage.width != 0f ) formatImage.width else d.intrinsicWidth.toFloat(),
231- if (formatImage.height != 0f ) formatImage.height else d.intrinsicHeight.toFloat(),
227+ if (imageWidth != 0f ) imageWidth else d.intrinsicWidth.toFloat(),
228+ if (imageHeight != 0f ) imageHeight else d.intrinsicHeight.toFloat(),
232229 d
233230 )
234231 drawable.addLevel(2 , 2 , insetDrawable)
235232 drawable.setBounds(
236233 0 , 0 ,
237- wh[0 ].toInt() + formatImage. marginLeft.toInt() + formatImage. marginRight.toInt(),
234+ wh[0 ].toInt() + marginLeft.toInt() + marginRight.toInt(),
238235 wh[1 ].toInt()
239236 )
240237 drawable.level = 2
@@ -324,7 +321,7 @@ class FormatTextView : BaseTextView {
324321 if (textSize > 0 ) {
325322 htmlBuilder.setSpan(
326323 AbsoluteSizeSpan (
327- Utils .sp2px(context, textSize.toFloat() ).toInt(),
324+ Utils .sp2px(context, textSize).toInt(),
328325 false
329326 ), start, end, flags
330327 )
0 commit comments