// 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)
复制代码