Tuesday, 8 October 2019

Dotted underline TextView not wrapping to the next line using SpannableString in Android

I done the dotted underline textview using this Dotted underline in TextView using SpannableString in Android. But dotted underline textview not wrapping to the next line. I have attached screenshot for reference. Please advice your ideas. Thanks

class DottedUnderlineSpan(mColor: Int, private val mSpan: String) : ReplacementSpan() {
    private val paint: Paint
    private var width: Int = 0
    private var spanLength: Float = 0f
    private val lengthIsCached = false
    internal var strokeWidth: Float = 0f
    internal var dashPathEffect: Float = 0f
    internal var offsetY: Float = 0f

    init {
        strokeWidth = 5f
        dashPathEffect = 4f
        offsetY = 14f
        paint = Paint()
        paint.color = mColor
        paint.style = Paint.Style.STROKE
        paint.pathEffect = DashPathEffect(floatArrayOf(dashPathEffect, dashPathEffect), 0f)
        paint.strokeWidth = strokeWidth
    }

    override fun getSize(paint: Paint, text: CharSequence, start: Int, end: Int, fm: Paint.FontMetricsInt?): Int {
        width = paint.measureText(text, start, end).toInt()
        return width
    }

    override fun draw(canvas: Canvas, text: CharSequence, start: Int,
                      end: Int, x: Float, top: Int, y: Int, bottom: Int, paint: Paint) {
        canvas.drawText(text, start, end, x, y.toFloat(), paint)

        if (!lengthIsCached)
            spanLength = paint.measureText(mSpan)
        val path = Path()
        path.moveTo(x, y + offsetY)
        path.lineTo(x + spanLength, y + offsetY)
        canvas.drawPath(path, this.paint)
    }
}

*Set dotted line using SpannableStringbuilder *

   DottedUnderlineSpan dottedUnderlineSpan = new DottedUnderlineSpan(underlineColor, dottedString);
                    strBuilder.setSpan(dottedUnderlineSpan, start, end, Spanned.SPAN_INCLUSIVE_INCLUSIVE);

Error:

Error facing here Expected: Expected



from Dotted underline TextView not wrapping to the next line using SpannableString in Android

No comments:

Post a Comment