pt123
pt123

Reputation: 2166

Android TextView getTextSize() does it include descenders, ascenders

I can't find anywhere there is a mention of how the getTextSize() in Textview is measured. From visual tests it doesn't seem to be including descenders, but seems to include ascenders. It doesn't appear to start exactly from the baseline also.

http://en.wikipedia.org/wiki/Descender

This is the closest mention of it but Romain Guy from Google just ignores that part of the question.

http://www.mail-archive.com/[email protected]/msg08514.html

As I need this as I am using Compound Drawables and I need to be able to align the drawable to the text on different devices.

Here is code I used to test on a compound drawable a circle that touches edges

tvData.setTextSize(TypedValue.COMPLEX_UNIT_PX, 100);
tvData.setText("agB5ãÂ");

int size = (int)tvData.getTextSize();
Drawable img = getResources().getDrawable(R.drawable.circle_white_full_72 ).mutate();
img.setBounds( 0 , 0 , size , size);
tvData.setCompoundDrawables( null, null, img, null );

Here is the result http://i.imgur.com/zUEzB.png

as you can see it doesn't seem to use the descenders and ascenders.

Here is the drawable image, if others want to test https://i.sstatic.net/cBrZF.png

When changing the image to 80% of the text size using

int size = (int)tvData.getTextSize() *80/100;

Here is the result, with the image transposed on top of the 100% image. Maybe setCompoundrawables is doing it's own scaling

enter image description here

I tried measuring midpoints of the font and drawable and it is off. Here is an image highlighting itenter image description here

Finally I moved the drawable 50pixels to the left, and then measured the output and it was half the height of the font text baseline to baseline, as the setTextSize was set at 100px.

Android must be using some other layout to scale and position the compound drawable. Maybe I should create another question for this. Here is an image highlighting baseline to baseline. enter image description here

Upvotes: 6

Views: 3208

Answers (1)

Kevin Coppock
Kevin Coppock

Reputation: 134704

From some light testing, it appears to be from ascent to descent (text size = descent - ascent). Did some debugging with a TextPaint to verify, and just to be a little more specific, I did:

Paint.FontMetricsInt metrics;
for(int i = 1; i < 100; i++) {
    mTextPaint.setTextSize(i);
    metrics = mTextPaint.getFontMetricsInt();
    if((metrics.descent - metrics.ascent) != i) Log.v("type", "Not equal");
}

And it remained true for each value.

Upvotes: 4

Related Questions