Android文字尺寸的測量

最近在自定義View中繪制文字的時候遇到需要測量文字的情況,查了一些資料,寫下以備用。

API介紹

FontMetrics API.PNG

字體有6個參數(shù):

  1. baseline
  2. ascent
  3. bottom
  4. descent
  5. leading
  6. top
baseline

基線的定義

The invisible line where all characters sit.

Definition: In typography, the baseline is the imaginary line upon which a line of text rests. In most typefaces, the descenders on characters such as g or p extend down below the baseline while curved letters such as c or o extend ever-so-slightly below the baseline. The baseline is the point from which other elements of type are measured including x-height and leading. The baseline is also significant in the alignment of drop caps and other page elements.
In typography and penmanship, the baseline is the line upon which most letters “sit” and below which descenders extend.
In the example to the right, the letter ‘p’ has a descender; the other letters sit on the (red) baseline.
Most, though not all, typefaces are similar in the following ways as regards the baseline:

  • capital letters sit on the baseline. The most common exceptions are the J and Q.
  • Lining figures (see Arabic numerals) sit on the baseline.
  • The following text figures have descenders: 3 4 5 7 9.
  • The following lowercase letters have descenders: g j p q y.
  • Glyphs with rounded lower extents (0 3 5 6 8 c C G J o O Q U) dip very slightly below the baseline (“overshoot”) to create the optical illusion that they sit on the baseline. Peter Karow’s Digital Typefaces suggests that typical overshoot is about 1.5%.
    The vertical distance of the base lines of consecutive lines in a paragraph is also known as line height or leading, although the latter can also refer to the baseline distance minus the font size.

基線是一條想象的線包裹著一個字符的上下兩端,但不包裹字符的“下擺”。

ascent

baseline到字體頂部的距離。

bottom

一個或多個字符中的最低的字符到baseline的距離,也可以視作所有字符的descent值的最大值。

descent

字符自baseline以下到最低處的距離。

leading

行距,可以理解為當(dāng)前行的所有字符的最低處到下一行字符的頂部的距離。

top

一個或多個字符中的最高處字符到baseline的距離,也可以視作所有字符的ascent的最大值。

文字各屬性示例1

獲取文字高度

文字各屬性示例2
文字各屬性示例3
文字各屬性示例4
文字各屬性示例5
文字各屬性示例6

方法一:

// 此方法沒有計算leading
Paint paint = new Paint();
// 設(shè)置字體大小
paint.setTextSize(xxx);
int height = paint.descent() - paint.ascent();

方法二:

Paint paint = new Paint();
// 設(shè)置字體大小
paint.setTextSize(xxx);
// 設(shè)置字體
paint.setTypeface(Typeface.xxx);
Paint.FontMetrics fontMetrics = paint.getFontMetrics();
float height1 = fontMetrics.descent - fontMetrics.ascent + fontMetrics.leading;
// height2測得的高度可能稍微比height1高一些
float height2 = fontMetrics.bottom - fontMetrics.top + fontMetrics.leading;

測量文字寬度

Paint paint = new Paint();
String str = "xxx";
// 設(shè)置字體大小
paint.setTextSize(xxx);
// 文字的寬度
float strWidth = paint.measureText(str);

通過文字所在的矩形區(qū)域獲得寬和高(不如measureText方法得到的值精確)

Paint paint = new Paint();
String str = "xxx";
Rect rect = new Rect();
// 參數(shù)1:需要測量的字符串
// 參數(shù)2:需要測量的字符串的第一個字符的索引
// 參數(shù)3:需要測量的字符串的最后一個字符的索引+1,通常是需要測量的字符串的長度
// 參數(shù)4:Rect對象
paint.getTextBounds(str, 0, str.length(), rect);
// 文字的寬度
int width = rect.width();
// 文字的高度
int height = rect.height();

參考:
1.Baseline
2.Android字符串進(jìn)階之三:字體屬性及測量(FontMetrics)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,854評論 0 10
  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 11,097評論 0 23
  • 棲梧雅舍閱讀 197評論 0 0
  • 夢見自己能看到兩個爸媽還有兩個男朋友,我跟他們講,總覺得自己撞邪了,可是他們毫無反應(yīng)。后來整條街的人都開始消失,家...
    白白月亮閱讀 231評論 0 0
  • 很多年以來,我一直站在一座樓的樓頂。 最開始,這座樓是初中時候的教學(xué)樓,眼前是陳舊的被鐵絲網(wǎng)護(hù)欄包裹的塑膠操場,邊...
    白色翅膀云閱讀 623評論 0 1

友情鏈接更多精彩內(nèi)容