通过Paint的getFontMetrics() 方法可以获得一个FontMetrics对象,它有如下的属性:
public float | ascent | The recommended distance above the baseline for singled spaced text. |
public float | bottom | The maximum distance below the baseline for the lowest glyph in the font at a given text size. |
public float | descent | The recommended distance below the baseline for singled spaced text. |
public float | leading | The recommended additional space to add between lines of text. |
public float | top | The maximum distance above the baseline for the tallest glyph in the font at a given text size. |
android开发中经常需要获取文字所占的大小,textview在被显示之前都是获取不到大小的,所以这里需要我们自己计算。
字符的宽度一般用textpaint的measureText(text)方法即可,高度的话需要计算ascent+descent,如果多行的情况还需要加上行间距。textpaint的getTextSize()方法获取的不是字体的高度。