Created
July 9, 2018 06:25
-
-
Save santaevpavel/61eb23092bdb8a926aeb8302ec95b4b6 to your computer and use it in GitHub Desktop.
Class for outlining text (TextView) on Android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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