How to find out text size when using Canvas.drawText ()

I use Canvas.drawText () to write text on a user view. I want to add a background 9 patch for text, so I need to know the borders of the text. The text is provided by the user, so it is not fixed.

Right now I'm using a monospace font, and I managed to create a size that doesn't look so bad, but in my hand I would prefer to use a different font.

+6
android canvas
source share
3 answers

You can use the public void getTextBounds(String text, int start, int end, Rect bounds) Paint method to get a rectangle that spans all of your text. Take a look at getTextBounds .

Hope this helps!

+9
source share

If I have a question correctly, then I think that getTextBounds(String text, int start, int end, Rect bounds) can do this for u .....

this method belongs to the Paint class ...

Refer to this link .....

Thanks....

+1
source share

You can also use measureText

 mTextTimeColorPaint.measureText(timeText) 

As in this post Text Center on the clock

+1
source share

All Articles