Reputation: 11651
I managed to center a text horizontaly with Align.CENTER. But it doesn't center the text vertically .
paint.setTextAlign(Align.CENTER);
canvas.drawText(text, fx, fy, paint);
How to center the text vertically ?
Upvotes: 3
Views: 827
Reputation: 289
StaticLayout yourLayout = new StaticLayout(text, mTextPaint,
canvas.getWidth(), Alignment.ALIGN_NORMAL, 1.0f, 0.0f,
false);
canvas.translate((canvas.getWidth() / 2) - (yourLayout.getWidth() / 2), (canvas.getHeight() / 2) - ((yourLayout.getHeight() / 2)));
This work for me.
Upvotes: 2
Reputation: 191
Align.CENTER
is for horizontal alignment. If you want to center vertically, you need to find the maximum height for the text and then position vertically within canvas by translating vertically using canvas.translate
Upvotes: 1