Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6026141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:24:10+00:00 2026-05-23T04:24:10+00:00

So I use Canvas.drawText to draw some string on a Canvas . The issue

  • 0

So I use Canvas.drawText to draw some string on a Canvas. The issue is that I want to draw a rectangle before it so that the text appears centred onto the rectangle. But I hit a real problem. The supplied x and y coordinates to drawText actually are not of the “top left” corner of the real text, but rather on the line where the characters begin. There is a method Paint.getTextBounds which returns a rectangle “with implied origin” at (0,0) of the text that would be drawn. The issue is that the origin is at (0,0). The width and the height of that box are correct but I don’t know how to place its top left corner at the top left corner of the string that is drawn on the canvas. I guess I should use FontMetrics, but since a lot of the values FontMetrics returns are undocumented I’m not really sure how to use them for my purpose.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T04:24:10+00:00Added an answer on May 23, 2026 at 4:24 am

    I ended up doing

    FontMetrics fm = new FontMetrics();
    paint.setTextAlign(Paint.Align.CENTER);
    paint.getFontMetrics(fm);
    canvas.drawText(text, x, y + -(fm.ascent + fm.descent) / 2, paint);
    

    Which actually draws the text centered at x, y. Before that I draw a rectangle centered at x, y with width paint.measureText(text)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a canvas.DrawText. I want to use gestures to dynamically change the text
I am trying to use the HTML5 canvas element to draw some arcs and
Use case: I've just entered insert mode, and typed some text. Now I want
I have a Canvas that i draw text on. (this is quite advanced i
I'm using the Canvas.drawText() to write text on a custom view. I want to
I want to fill the area outside a rectangle on a canvas. I use
I want to use Canvas.TextRect to write something on the canvas with 90 degree
I ran into an interesting issue today. I have canvas elements that I am
I can make a obj to use the canvas to draw like this: MyObj.myDiv
I am looking to look at some examples of projects that makes heavy use

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.