String text = "5";
float fitw = 100.0f;
mTextPaint.setTextSize(fitw * (mTextPaint.getTextSize() / mTextPaint.measureText(text)));
How to derive textsize from a character's width and font
-
12-12-2021 - |
Question
Say I want to draw the string "5" to the screen. I've created a Paint
object for this with the default android font.
I know how to measure text like this:
mTextPaint.measureText(...params...)
to get the width of the text in float format.
But now I want to do this backwards. I have calculated the maximum width (float) that the character ('5') may use.
How do I find the right textSize using: the string or character ("5"), the Paint
object for drawing the text and the maximum width I want to allow the character to take up?
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow