Hi,
I have a method that is passed a text string. For a specified font and size, it works out the bounding box for the text and then draws a roundrect that will contain it.
I'm trying to keep all of these roundrects the same height (irrespective of ascenders and descenders in the font), so the basic getTextBounds isn't quite enough as it bounds the specific text passed.
The solution I came up with was to use getTextBounds to work out the width of the text, and ascent/descent from FontMetricsInt to work out the height:
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setTextSize(fontsize);
paint.setTypeface(Typeface.SANS_SERIF);
Rect textBounds = new Rect();
paint.getTextBounds(value, 0, value.length(), textBounds);
FontMetricsInt fm = paint.getFontMetricsInt();
this.width = textBounds.width();
this.height = fm.descent - fm.ascent;
this.textAscent = fm.ascent;
Is there a simpler way?
Edits: Finally figured out how to format the code properly!