// text 文字內容,
// x 和 y 是文字的座標。
drawText(String text, float x, float y, Paint paint);
複製代碼
注意:這個座標並非文字的左上角,而是一個與左下角比較接近的位置。 canvas
![]()
y 值爲基線 bash
![]()
//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);
複製代碼
setTextSize(float textSize);
複製代碼
setTypeface(Typeface typeface);
複製代碼
例:post
// 使用系統自帶
setTypeface(Typeface.SERIF);
// 使用ass
paint.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "Satisfy-Regular.ttf"));
複製代碼
setFakeBoldText(boolean fakeBoldText);
複製代碼
setStrikeThruText(boolean strikeThruText);
複製代碼
setUnderlineText(boolean underlineText);
複製代碼
setTextSkewX(float skewX);
複製代碼
setTextScaleX(float scaleX);
複製代碼
setLetterSpacing(float letterSpacing);
複製代碼
setFontFeatureSettings(String settings);
複製代碼
例:字體
paint.setFontFeatureSettings("smcp"); // 設置 "small caps"
複製代碼
setTextAlign(Paint.Align align)
複製代碼
三種對其方式: 默認值爲 LEFT。ui
setTextLocale(Locale locale) / setTextLocales(LocaleList locales)
複製代碼
例:spa
paint.setTextLocale(Locale.CHINA); // 簡體中文
paint.setTextLocale(Locale.TAIWAN); // 繁體中文
paint.setTextLocale(Locale.JAPAN); // 日語
複製代碼
setHinting(int mode);
複製代碼
float getFontSpacing();
複製代碼
FontMetircs getFontMetrics();
// 頻繁獲取 FontMetrics 的時候
FontMetircs getFontMetrics(FontMetrics fontMetrics);
複製代碼
FontMetircs 中有 ascent, descent, top, bottom, leading。 code
注:對文字手動換行繪製,多數時候應該選取 getFontSpacing() 來獲得行距,不但使用更簡單,顯示效果也會更好。cdn
// 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);
複製代碼
float measureText(String text);
複製代碼
與 getTextBounds 區別blog
// 等價於對字符串中的每一個字符分別調用 measureText();
getTextWidths(String text, float[] widths);
複製代碼
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);
複製代碼
// start end 是文字的起始和結束座標;
// contextStart contextEnd 是上下文的起始和結束座標;
// isRtl 是文字的方向;
// offset 是字數的偏移,即計算第幾個字符處的光標。
getRunAdvance(CharSequence text, int start, int end, int contextStart, int contextEnd, boolean isRtl, int offset);
複製代碼
// text 是要測量的文字;
// start end 是文字的起始和結束座標;
// contextStart contextEnd 是上下文的起始和結束座標;
// isRtl 是文字方向;
// advance 是給出的位置的像素值。填入參數,對應的字符偏移量將做爲返回值返回。
getOffsetForAdvance(CharSequence text, int start, int end, int contextStart, int contextEnd, boolean isRtl, float advance);
複製代碼
getOffsetForAdvance() 配合上 getRunAdvance() 一塊兒使用,就能夠實現「獲取用戶點擊處的文字座標」的需求。
hasGlyph(String string)
複製代碼