Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android TextView What does elegantTextHeight do? (API 21)

I've come across the elegantTextHeight attribute for a TextView but don't see any changes when I play with it.

What does it do? And how do I use it?

like image 295
SalicBlu3 Avatar asked Aug 05 '15 01:08

SalicBlu3


People also ask

How do you put line through text on android?

If you want to show a strike-through text you can do it programming using PaintFlags. You can set paint flags Paint. STRIKE_THRU_TEXT_FLAG to a TextView and it will add a strike-through to the text. TextView textView = (TextView) findViewById(R.

How do I know if my TextView is clicked?

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

What is lineSpacingExtra?

The difference is that android:lineSpacingExtra add extra spacing between lines of text of TextView and android:lineSpacingMultiplier work as scale factor for height of line space. in other words, each line height will be height*multiplier + extra. Follow this answer to receive notifications.


2 Answers

Short answer: It's a method to turn off the "font compacting" optimization for some languages. And if you don't know what I'm saying, it's probably because your language doesn't have the "font compacting" optimization, which means you don't have to care about it.

Long answer:

Take a look at the doc first:

Set the paint's elegant height metrics flag. This setting selects font variants that have not been compacted to fit Latin-based vertical metrics, and also increases top and bottom bounds to provide more space.

What does it mean?

In some languages, some glyphs could be very tall. Like this:

Thai + Latin

(Those are characters I randomly picked from Thai Alphabet, if you're curious.)

Look at the texts on the left. Really tall, huh? But the fact is, it's not tall enough. It should've been taller.

Fonts usually have 2 variants for "tall text" languages: an original variant, and a compacted variant. The compacted version is to ensure texts won't look weird when texts of different languages are laid together. The default version (at least in Android's Paint) is the compacted version, as shown in the screenshot above.

But sometimes you may need the original variant. If you need it, add this line:

paint.setElegantTextHeight(true);

Then your texts will be drawn with the original (elegant) variant.Original variant

The Thai texts become taller, see?

So, things are clear now: Some languages could have very tall glyphs, and they're compacted by default. And setElegantTextHeight(true) will:

  1. Switch to the original, non-compacted, elegant variant to draw texts.
  2. Increase the top and bottom bounds to provide more space for the taller texts.
like image 76
rengwuxian Avatar answered Oct 19 '22 05:10

rengwuxian


From the docs:

Set the paint's elegant height metrics flag. This setting selects font variants that have not been compacted to fit Latin-based vertical metrics, and also increases top and bottom bounds to provide more space.

like image 28
N.T. Avatar answered Oct 19 '22 05:10

N.T.