經常使用自定義 View 方法彙總 Text 相關(三)

1. canvas 中

1.1 文字繪製

// text 文字內容,
// x 和 y 是文字的座標。
drawText(String text, float x, float y, Paint paint);
複製代碼

注意:這個座標並非文字的左上角,而是一個與左下角比較接近的位置。 canvas

y 值爲基線 bash

1.2 多行文字 StaticLayout

//width 是文字區域的寬度,文字到達這個寬度後就會自動換行;
// align 是文字的對齊方向;
// spacingmult 是行間距的倍數,一般狀況下填 1 就好;
// spacingadd 是行間距的額外增長值,一般狀況下填 0 就好;
// includeadd 是指是否在文字上下添加額外的空間,來避免某些太高的字符的繪製出現越界。
StaticLayout(CharSequence source, TextPaint paint, int width, Layout.Alignment align, float spacingmult, float spacingadd, boolean includepad)
// 調用
staticLayout1.draw(canvas);
複製代碼

2. paint 中的設置

2.1 設置文字大小

setTextSize(float textSize);
複製代碼

2.2 設置文字字體

setTypeface(Typeface typeface);
複製代碼

例:post

// 使用系統自帶
setTypeface(Typeface.SERIF);
// 使用ass
paint.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "Satisfy-Regular.ttf"));
複製代碼

2.3 是否使用僞粗體

setFakeBoldText(boolean fakeBoldText);
複製代碼

2.4 是否加刪除線。

setStrikeThruText(boolean strikeThruText);
複製代碼

2.5 是否加下劃線。

setUnderlineText(boolean underlineText);
複製代碼

2.6 設置文字橫向錯切角度

setTextSkewX(float skewX);
複製代碼

2.7 設置文字橫向放縮。也就是文字變胖變瘦。

setTextScaleX(float scaleX);
複製代碼

2.8 設置字符間距。默認值是 0。

setLetterSpacing(float letterSpacing);
複製代碼

2.9 用 CSS 的 font-feature-settings 的方式來設置文字。

setFontFeatureSettings(String settings);
複製代碼

例:字體

paint.setFontFeatureSettings("smcp"); // 設置 "small caps"
複製代碼

2.10 設置文字的對齊方式

setTextAlign(Paint.Align align)
複製代碼

三種對其方式: 默認值爲 LEFT。ui

  • Paint.Align.LEFT
  • Paint.Align.CETNER
  • Paint.Align.RIGHT

2.11 設置繪製所使用的 Locale

setTextLocale(Locale locale) / setTextLocales(LocaleList locales)
複製代碼

例:spa

paint.setTextLocale(Locale.CHINA); // 簡體中文
paint.setTextLocale(Locale.TAIWAN); // 繁體中文
paint.setTextLocale(Locale.JAPAN); // 日語
複製代碼

2.12 設置是否啓用字體的 hinting (字體微調)。

setHinting(int mode);
複製代碼

2.13 獲取推薦的行距。兩行文字的 baseline 的距離。

float getFontSpacing();
複製代碼

2.14 獲取 Paint 的 FontMetrics。

FontMetircs getFontMetrics();
// 頻繁獲取 FontMetrics 的時候
FontMetircs getFontMetrics(FontMetrics fontMetrics);
複製代碼

FontMetircs 中有 ascent, descent, top, bottom, leading。 code

leading:上行的 bottom 線和下行的 top 線的距離,也就是上圖中第一行的紅線和第二行的藍線的距離

注:對文字手動換行繪製,多數時候應該選取 getFontSpacing() 來獲得行距,不但使用更簡單,顯示效果也會更好。cdn

2.15 獲取文字的顯示範圍。

// text 是要測量的文字,
// start 和 end 分別是文字的起始和結束位置,
// bounds 是存儲文字顯示範圍的對象,方法在測算完成以後會把結果寫進 bounds。
getTextBounds(String text, int start, int end, Rect bounds);
getTextBounds(char[] text, int index, int count, Rect bounds);
複製代碼

例:對象

paint.getTextBounds(text, 0, text.length(), bounds);
bounds.left += offsetX;
bounds.top += offsetY;
bounds.right += offsetX;
bounds.bottom += offsetY;
paint.setStyle(Paint.Style.STROKE);
canvas.drawRect(bounds, paint);
複製代碼

2.16 測量文字的寬度並返回。

float measureText(String text);
複製代碼

與 getTextBounds 區別blog

  • getTextBounds 獲取最小的尺寸
  • measureText 還有邊距

2.17 獲取字符串中每一個字符的寬度,並把結果填入參數 widths。

// 等價於對字符串中的每一個字符分別調用 measureText();
getTextWidths(String text, float[] widths);
複製代碼

2.18 來測量文字寬度的

int breakText(String text, boolean measureForwards, float maxWidth, float[] measuredWidth);
複製代碼

例:

int measuredCount;
float[] measuredWidth = {0};
// 寬度上限 300 (不夠用,截斷)
measuredCount = paint.breakText(text, 0, text.length(), true, 300, measuredWidth);
canvas.drawText(text, 0, measuredCount, 150, 150, paint);
// 寬度上限 400 (不夠用,截斷)
measuredCount = paint.breakText(text, 0, text.length(), true, 400, measuredWidth);
canvas.drawText(text, 0, measuredCount, 150, 150 + fontSpacing, paint);
// 寬度上限 500 (夠用)
measuredCount = paint.breakText(text, 0, text.length(), true, 500, measuredWidth);
canvas.drawText(text, 0, measuredCount, 150, 150 + fontSpacing * 2, paint);
// 寬度上限 600 (夠用)
measuredCount = paint.breakText(text, 0, text.length(), true, 600, measuredWidth);
canvas.drawText(text, 0, measuredCount, 150, 150 + fontSpacing * 3, paint);
複製代碼

2.19 計算出某個字符處光標的 x 座標

//  start end 是文字的起始和結束座標;
// contextStart contextEnd 是上下文的起始和結束座標;
// isRtl 是文字的方向;
// offset 是字數的偏移,即計算第幾個字符處的光標。
getRunAdvance(CharSequence text, int start, int end, int contextStart, int contextEnd, boolean isRtl, int offset);
複製代碼

2.20 給出一個位置的像素值,計算出文字中最接近這個位置的字符偏移量(即第幾個字符最接近這個座標)。

// text 是要測量的文字;
// start end 是文字的起始和結束座標;
// contextStart contextEnd 是上下文的起始和結束座標;
// isRtl 是文字方向;
// advance 是給出的位置的像素值。填入參數,對應的字符偏移量將做爲返回值返回。
getOffsetForAdvance(CharSequence text, int start, int end, int contextStart, int contextEnd, boolean isRtl, float advance);
複製代碼

getOffsetForAdvance() 配合上 getRunAdvance() 一塊兒使用,就能夠實現「獲取用戶點擊處的文字座標」的需求。

2.21 檢查指定的字符串中是不是一個單獨的字形 (glyph)。

hasGlyph(String string)
複製代碼

注:文章參考:HenCoder Android 開發進階:自定義 View 1-3 文字的繪製

相關文章
相關標籤/搜索