Skip to content

Instantly share code, notes, and snippets.

@santaevpavel
Created July 9, 2018 06:25
Show Gist options
  • Save santaevpavel/61eb23092bdb8a926aeb8302ec95b4b6 to your computer and use it in GitHub Desktop.
Save santaevpavel/61eb23092bdb8a926aeb8302ec95b4b6 to your computer and use it in GitHub Desktop.
Class for outlining text (TextView) on Android
class OutlineSpan(
@ColorInt private val strokeColor: Int,
@Dimension private val strokeWidth: Float
): ReplacementSpan() {
override fun getSize(
paint: Paint,
text: CharSequence,
start: Int,
end: Int,
fm: Paint.FontMetricsInt?
): Int {
return paint.measureText(text.toString().substring(start until end)).toInt()
}
override fun draw(
canvas: Canvas,
text: CharSequence,
start: Int,
end: Int,
x: Float,
top: Int,
y: Int,
bottom: Int,
paint: Paint
) {
val originTextColor = paint.color
paint.apply {
color = strokeColor
style = Paint.Style.STROKE
this.strokeWidth = this@OutlineSpan.strokeWidth
}
canvas.drawText(text, start, end, x, y.toFloat(), paint)
paint.apply {
color = originTextColor
style = Paint.Style.FILL
}
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