Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I finally found a more in depth example of the use of LineHeightSpan... Actually LineHeightSpan.WithDensity to be more precise... The following is the excerpt that helped me to resolve my issue:</p> <pre><code>private static class Height implements LineHeightSpan.WithDensity { private int mSize; private static float sProportion = 0; public Height(int size) { mSize = size; } public void chooseHeight(CharSequence text, int start, int end, int spanstartv, int v, Paint.FontMetricsInt fm) { // Should not get called, at least not by StaticLayout. chooseHeight(text, start, end, spanstartv, v, fm, null); } public void chooseHeight(CharSequence text, int start, int end, int spanstartv, int v, Paint.FontMetricsInt fm, TextPaint paint) { int size = mSize; if (paint != null) { size *= paint.density; } if (fm.bottom - fm.top &lt; size) { fm.top = fm.bottom - size; fm.ascent = fm.ascent - size; } else { if (sProportion == 0) { /* * Calculate what fraction of the nominal ascent * the height of a capital letter actually is, * so that we won't reduce the ascent to less than * that unless we absolutely have to. */ Paint p = new Paint(); p.setTextSize(100); Rect r = new Rect(); p.getTextBounds("ABCDEFG", 0, 7, r); sProportion = (r.top) / p.ascent(); } int need = (int) Math.ceil(-fm.top * sProportion); if (size - fm.descent &gt;= need) { /* * It is safe to shrink the ascent this much. */ fm.top = fm.bottom - size; fm.ascent = fm.descent - size; } else if (size &gt;= need) { /* * We can't show all the descent, but we can at least * show all the ascent. */ fm.top = fm.ascent = -need; fm.bottom = fm.descent = fm.top + size; } else { /* * Show as much of the ascent as we can, and no descent. */ fm.top = fm.ascent = -size; fm.bottom = fm.descent = 0; } } } } </code></pre> <p>This was taken from <a href="http://alvinalexander.com/java/jwarehouse/android/core/java/android/content/res/StringBlock.java.shtml">this example</a>.</p> <p>What it does is as quoted below:</p> <blockquote> <p>Forces the text line to be the specified height, shrinking/stretching the ascent if possible, or the descent if shrinking the ascent further will make the text unreadable.</p> </blockquote> <p>I hope this helps the next person :-)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload