Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 http://i.imgur.com/Yhf8b.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

like image 949
pt123 Avatar asked Dec 27 '12 23:12

pt123


People also ask

What is text view in android?

TextView is the user interface which displays the text message on the screen to the user. It is based on the layout size, style, and color, etc. TextView is used to set and display the text according to our specifications.

How do I change line spacing in Android?

On your Android phone or tablet, open a document in the Google Docs app. Double-tap the place in your document you want to edit. tap Paragraph. Next to "Line spacing," use the arrows to choose the amount of space you want between the lines in the paragraph.

What is includeFontPadding?

You can use android:includeFontPadding="false" to get rid of the default padding in a TextView . Android docs say this about the attribute: Leave enough room for ascenders and descenders instead of using the font ascent and descent strictly.

How do I know if my TextView is clicked?

Inside the onClick(View v) implementation add: v. getId(); To determine whice TextView pressed.


1 Answers

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.

like image 98
Kevin Coppock Avatar answered Sep 22 '22 04:09

Kevin Coppock