android FontMetrics中各個字段的意義

FontMetrics類的定義如下:

/**
     * Class that describes the various metrics for a font at a given text size.
     * Remember, Y values increase going down, so those values will be positive,
     * and values that measure distances going up will be negative. This class
     * is returned by getFontMetrics().
     */
public static class FontMetrics {
        /**
         * The maximum distance above the baseline for the tallest glyph in
         * the font at a given text size.
         */
        public float   top;
        /**
         * The recommended distance above the baseline for singled spaced text.(負值)
         */
        public float   ascent;
        /**
         * The recommended distance below the baseline for singled spaced text.(正值)
         */
        public float   descent;
        /**
         * The maximum distance below the baseline for the lowest glyph in
         * the font at a given text size.
         */
        public float   bottom;
        /**
         * The recommended additional space to add between lines of text.
         */
        public float   leading;
}

用下圖表示這5個變量的意思:
在這裏插入圖片描述

  • Baseline是基線,在Android中,文字的繪製都是從Baseline處開始的

  • leading(行間距)則表示上一行字符的descent到該行字符的ascent之間的距離;

  • top和bottom文檔描述地很模糊,其實這裏我們可以借鑑一下TextView對文本的繪製,TextView在繪製文本的時候總會在文本的最外層留出一些內邊距,爲什麼要這樣做?因爲TextView在繪製文本的時候考慮到了類似讀音符號,下圖中的A上面的符號就是一個拉丁文的類似讀音符號的東西:
    在這裏插入圖片描述
    top的意思其實就是除了Baseline到字符頂端的距離外還應該包含這些符號的高度,bottom的意思也是一樣。在TextView中我們可以通過xml設置其屬性android:includeFontPadding="false"去掉一定的邊距值但是不能完全去掉。

參考:

  1. 用TextPaint來繪製文字. https://www.cnblogs.com/tianzhijiexian/p/4297664.html
  2. 聊一聊Paint.FontMetrics.descent和Paint.FontMetrics.ascent那些事. https://www.jianshu.com/p/7a29d171327f
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章