美文网首页程序员
Android文字尺寸的测量

Android文字尺寸的测量

作者: enGrave93 | 来源:发表于2018-06-22 21:00 被阅读37次

最近在自定义View中绘制文字的时候遇到需要测量文字的情况,查了一些资料,写下以备用。

API介绍

FontMetrics API.PNG

字体有6个参数:

  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

行距,可以理解为当前行的所有字符的最低处到下一行字符的顶部的距离。

top

一个或多个字符中的最高处字符到baseline的距离,也可以视作所有字符的ascent的最大值。

文字各属性示例1

获取文字高度

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

方法一:

// 此方法没有计算leading
Paint paint = new Paint();
// 设置字体大小
paint.setTextSize(xxx);
int height = paint.descent() - paint.ascent();

方法二:

Paint paint = new Paint();
// 设置字体大小
paint.setTextSize(xxx);
// 设置字体
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";
// 设置字体大小
paint.setTextSize(xxx);
// 文字的宽度
float strWidth = paint.measureText(str);

通过文字所在的矩形区域获得宽和高(不如measureText方法得到的值精确)

Paint paint = new Paint();
String str = "xxx";
Rect rect = new Rect();
// 参数1:需要测量的字符串
// 参数2:需要测量的字符串的第一个字符的索引
// 参数3:需要测量的字符串的最后一个字符的索引+1,通常是需要测量的字符串的长度
// 参数4:Rect对象
paint.getTextBounds(str, 0, str.length(), rect);
// 文字的宽度
int width = rect.width();
// 文字的高度
int height = rect.height();

参考:
1.Baseline
2.Android字符串进阶之三:字体属性及测量(FontMetrics)

相关文章

  • Android文字尺寸的测量

    最近在自定义View中绘制文字的时候遇到需要测量文字的情况,查了一些资料,写下以备用。 API介绍 字体有6个参数...

  • Android尺寸相关

    概念 屏幕尺寸 按屏幕对角测量的实际物理尺寸。为简便起见,Android 将所有实际屏幕尺寸分组为四种通用尺寸:小...

  • 2. 浅谈Android中的屏幕适配

    几组概念 屏幕尺寸: 按屏幕对角测量的实际物理尺寸。为简便起见,Android 将所有实际屏幕尺寸分组为四种通用尺...

  • android text 测量尺寸相关

    一、获取推荐的行距 paint.getFontSpacing() 即获取推荐的两行文字的baseline之间的距离...

  • Paint文字相关

    文字相关:显示效果、测量文字尺寸、光标、检查字形 设置显示效果类 setTextSize(float textSi...

  • 酷炫文字变色

    基本api 1、测量文字尺寸 2、文字绘制方式 3、文字绘制显示效果 实现 1、设置全局变量 2、paint的初始...

  • 技术博文收藏推荐篇

    1、【Android】TextView的文字长度测量及各种padding解析2、android studio gr...

  • daily 9.21-9.25

    day 1 1.使用opencv测量物体的尺寸openCV测量物体的尺寸[https://zhuanlan.zhi...

  • 一些小的心得0728

    测量零件尺寸时,一定要测量完全,自己对着草图,看能不能画出来准确尺寸,找到没有定位的尺寸链。 测量时考虑加工误差,...

  • 你的浏览器尺寸有多大?试试这5款免费的浏览器尺寸测量工具吧

    测量的是浏览器的什么地方的尺寸? 此类工具主要是测量浏览器内容窗口的大小,不包含浏览器顶部的工具栏尺寸。 测量尺寸...

网友评论

    本文标题:Android文字尺寸的测量

    本文链接:https://www.haomeiwen.com/subject/ayhlyftx.html