经常使用自定义 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 文字的绘制

相关文章
相关标签/搜索