Android Center Text on Canvas

Android Center text on canvas

Try the following:

 Paint textPaint = new Paint();
textPaint.setTextAlign(Paint.Align.CENTER);

int xPos = (canvas.getWidth() / 2);
int yPos = (int) ((canvas.getHeight() / 2) - ((textPaint.descent() + textPaint.ascent()) / 2)) ;
//((textPaint.descent() + textPaint.ascent()) / 2) is the distance from the baseline to the center.

canvas.drawText("Hello", xPos, yPos, textPaint);

center text vertically and horizontally with canvas

The problem is that you're currently not considering the width and height of the text. If your text is "hello" or "THIS A STRING" you just treat it equally, so that's wrong.

You need to calculate the width and the height of the text, and then shift the text position by half those distances.

For example to center vertically:

Rect r = new Rect();
paint.getTextBounds(text, 0, text.length(), r);
yPos += (Math.abs(r.height()))/2; // or maybe -= instead of +=, depends on your coordinates

I hope this takes you into the right direction.

EDIT:
The origin is interpreted based on the Align setting in the paint. You're using

 textPaint.setTextAlign(Align.CENTER);

So you probably don't need to do any calculations to center horizontally (meaning that for horizontal origin you should use the code you already had).

 int xPos = (canvas.getWidth() / 2);

Center text in Canvas Android

this may help you...

public static void drawText(Canvas canvas, Paint paint, String text) {
Rect bounds = new Rect();
paint.getTextBounds(text, 0, text.length(), bounds);
int x = (canvas.getWidth() / 2) - (bounds.width() / 2);
int y = (canvas.getHeight() / 2) - (bounds.height() / 2);
canvas.drawText(text, x, y, paint);
}

How to center a text in canvas vertically

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

How to center text horizontally on Canvas

Its actually simple. All you need to do is to use the Paint.measureText() method to get the width of the text, divide by 2 to get its half and then shift it to the left to center it.

Take a look at this. I've created two float variables that hold the width of each text on Canvas:

float topTextMeasurement = topPaint.measureText(topText);
float bottomTextMeasurement = bottomPaint.measureText(bottomText);

Then I've done the above adjustment in your Canvas.drawText() methods' x parameters.

canvas.drawText(topText, topX - (topTextMeasurement/2), 200, topPaint);
canvas.drawText(bottomText, bottomX - (bottomTextMeasurement/2), canvas.getHeight() - 200, bottomPaint);

But this is only if your text is not going to exceed a single line. For multi-line texts, I suggest that you look into DynamicLayout

StaticLayout positioning center of canvas not working like canvas.drawText?

I figured out the issue guys. It was because i was ignoring the width of the StaticLayout text's width while placing it to the centre.

Here I'm giving the StaticLayout the entire width of the View.

    staticLayout = StaticLayout(
"Hello world is helloworld",
ststicTextPaint,
w,
Layout.Alignment.ALIGN_CENTER,
1f,
0f,
false
)

While drawing the view

staticLayout.draw(canvas, ((width / 2)-staticLayout.width/2).toFloat(), (height / 2).toFloat())

I substracted the half of the width of the `StaticLayout width so that it'll place in the exact centre.
Sample Image

Not able to center text in android canvas

 paint1.setTextAlign(Align.CENTER);
String t1="N";
int ttwidth=determineMaxTextSize(t1, mat/4);
paint1.setTextSize(ttwidth);
Rect bounds = new Rect();
paint1.getTextBounds(t1, 0, 3, bounds);
c.drawCircle(swidth/4, height+height/2, mat/4, paint);
c.drawText(t1, swidth/4, height+height/2+bounds.height()/2, paint1);

Above code is the answer

How to center Text in Canvas in Jetpack compose?

You can do it by measuring text and placing it as

@OptIn(ExperimentalTextApi::class)
@Composable
fun MyCenterTextInCanvas() {
val width: Dp = 200.dp
val height: Dp = 40.dp
val textMeasurer = rememberTextMeasurer()

val textLayoutResult: TextLayoutResult =
textMeasurer.measure(text = AnnotatedString("Sample Text"))
val textSize = textLayoutResult.size
Canvas(
modifier = Modifier
.background(Color.LightGray)
.requiredSize(
width = width,
height = height,
),
) {

val canvasWidth = size.width
val canvasHeight = size.height

drawText(
textMeasurer = textMeasurer,
text = "Sample Text",
topLeft = Offset(
(canvasWidth - textSize.width) / 2f,
(canvasHeight - textSize.height) / 2f
),
)
}
}

Sample Image



Related Topics



Leave a reply



Submit