Is there any way to get the linespacing of a TextView
in Android
? I try to find fontMetrics
of the Paint
of the TextView
and do like this:
tv1.getPaint().getFontMetrics(pfm);
float fontTop = pfm.top;
float fontBottom = pfm.bottom;
System.out.println("Line Space >> " + (fontBottom - fontTop));
But it seems that result is the same until I change font size of the TextView
. So how can I get the linespacing of a TextView
?
Linespacing of TextView
is
textView.getPaint().getFontSpacing() * textView.getLineSpacingMultiplier() + textView.getLineSpacingExtra();
Note getLineSpacingMultiplier()
and getLineSpacingExtra()
methods are available since API 16+.
getLineSpacingExtra ()
; Gets the line spacing extra space
tv1.getLineSpacingExtra ()
textView.setLineSpacing()
or from xml you can use android:lineSpacingExtra
for setting the extra space.
try {
Field mSpacingAddField = TextView.class.getDeclaredField("mSpacingAdd");
Field mSpacingMultField = TextView.class.getDeclaredField("mSpacingMult");
mSpacingAddField.setAccessible(true);
mSpacingMultField.setAccessible(true);
mSpacingAdd = mSpacingAddField.getFloat(textView);
mSpacingMult = mSpacingMultField.getFloat(textView);
} catch (Exception e) {
e.printStackTrace();
}