Is it possible to pass Html to Canvas.drawtext()
.
I tried this:
canvas.drawText(Html.fromHtml("This is an <u>underline</u> text demo for TextView."), 0, 20, colIndex, rowIndex, getTextPaint());
But that cannot parse tags and not show correctly.
Thanks in advance.
To add a text to the HTML <canvas> element, you need to use the fillText() or strokeText() methods, or you can use the combination of the two. You can also give color to your text, define the font and size, and even add gradients.
Yes of course you can write a text on canvas with ease, and you can set the font name, font size and font color. There are two method to build a text on Canvas, i.e. fillText() and strokeText().
You can parse the html string into Spanned
public static Spanned fromHtml(String html, int flags) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
return Html.fromHtml(html, flags);
} else {
return Html.fromHtml(html);
}
}
then use StaticLayout to draw:
mStaticLayout = new StaticLayout(fromHtml(html, Html.FROM_HTML_MODE_LEGACY), mPaint, sizeWidth, Alignment.ALIGN_NORMAL, 1.0f, 0, false);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With