Skip to content

Instantly share code, notes, and snippets.

@masoudkarimi
Last active September 19, 2020 18:18
Show Gist options
  • Save masoudkarimi/257ee548d96d122606a68572347f635e to your computer and use it in GitHub Desktop.
Save masoudkarimi/257ee548d96d122606a68572347f635e to your computer and use it in GitHub Desktop.
Padding Right Span
class PaddingRightSpan(private val mPadding: Int) : ReplacementSpan() {
override fun getSize(
paint: Paint, text: CharSequence,
start: Int, end: Int,
fm: Paint.FontMetricsInt?
): Int {
val widths = FloatArray(end - start)
paint.getTextWidths(text, start, end, widths)
var sum = 0
for (i in widths.indices) {
sum += widths[i].toInt()
}
return sum + mPadding
}
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)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment