FontMetrics ----- 绘制文本,获取文本高度

Canvas 绘制文本时,使用FontMetrics对象,计算位置的坐标。

 1 public static class FontMetrics {
 2     /**
 3      * The maximum distance above the baseline for the tallest glyph in
 4      * the font at a given text size.
 5      */
 6     public float   top;
 7     /**
 8      * The recommended distance above the baseline for singled spaced text.
 9      */
10     public float   ascent;
11     /**
12      * The recommended distance below the baseline for singled spaced text.
13      */
14     public float   descent;
15     /**
16      * The maximum distance below the baseline for the lowest glyph in
17      * the font at a given text size.
18      */
19     public float   bottom;
20     /**
21      * The recommended additional space to add between lines of text.
22      */
23     public float   leading;
24 }

它的各基准线可以参考下图:

 1 /** 绘制FontMetrics对象的各种线 */
 2 mPaint.reset();
 3 mPaint.setColor(Color.WHITE);
 4 mPaint.setTextSize(80);
 5 // FontMetrics对象
 6 FontMetrics fontMetrics = mPaint.getFontMetrics();
 7 String text = "abcdefg";
 8 // 计算每一个坐标
 9 float textWidth = mPaint.measureText(text);
10 float baseX = 30;
11 float baseY = 700;
12 float topY = baseY + fontMetrics.top;
13 float ascentY = baseY + fontMetrics.ascent;
14 float descentY = baseY + fontMetrics.descent;
15 float bottomY = baseY + fontMetrics.bottom;
16 // 绘制文本
17 canvas.drawText(text, baseX, baseY, mPaint);
18 // BaseLine描画
19 mPaint.setColor(Color.RED);
20 canvas.drawLine(baseX, baseY, baseX + textWidth, baseY, mPaint);
21 mPaint.setTextSize(20);
22 canvas.drawText("base", baseX + textWidth, baseY, mPaint);
23 // Base描画
24 canvas.drawCircle(baseX, baseY, 5, mPaint);
25 // TopLine描画
26 mPaint.setColor(Color.LTGRAY);
27 canvas.drawLine(baseX, topY, baseX + textWidth, topY, mPaint);
28 canvas.drawText("top", baseX + textWidth, topY, mPaint);
29 // AscentLine描画
30 mPaint.setColor(Color.GREEN);
31 canvas.drawLine(baseX, ascentY, baseX + textWidth, ascentY, mPaint);
32 canvas.drawText("ascent", baseX + textWidth, ascentY + 10, mPaint);
33 // DescentLine描画
34 mPaint.setColor(Color.YELLOW);
35 canvas.drawLine(baseX, descentY, baseX + textWidth, descentY, mPaint);
36 canvas.drawText("descent", baseX + textWidth, descentY, mPaint);
37 // ButtomLine描画
38 mPaint.setColor(Color.MAGENTA);
39 canvas.drawLine(baseX, bottomY, baseX + textWidth, bottomY, mPaint);
40 canvas.drawText("buttom", baseX + textWidth, bottomY + 10, mPaint);

相信通过以上程序,能够很好的理解topLine,buttomLine,baseLine,ascentLine,descentLine。

另外:Paint类有两个方法

 1 /**
 2  * Return the distance above (negative) the baseline (ascent) based on the
 3  * current typeface and text size.
 4  *
 5  * @return the distance above (negative) the baseline (ascent) based on the
 6  *         current typeface and text size.
 7  */
 8 public native float ascent();
 9
10 /**
11  * Return the distance below (positive) the baseline (descent) based on the
12  * current typeface and text size.
13  *
14  * @return the distance below (positive) the baseline (descent) based on
15  *         the current typeface and text size.
16  */
17 public native float descent();

ascent():the distance above the baseline(baseline以上的height)

descent():the distance below the baseline(baseline以下的height)

所以ascent() + descent() 可以看成文字的height。

到此为止,怎么获取文字的height和width都已经揭晓了:

获取height : mPaint.ascent() + mPaint.descent()

获取width : mPaint.measureText(text)

时间: 2024-07-29 21:27:30

FontMetrics ----- 绘制文本,获取文本高度的相关文章

jquery设置文本框值 与获取文本框的值

<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <script src="../js/jquery-1.12.4.js"></script> </head> <body> <input type="tex

iOS 动态计算文本内容的高度

关于ios 下动态计算文本内容的高度,经过查阅和网上搜素,现在看到的有以下几种方法: 1. //  获取字符串的大小  ios6 - (CGSize)getStringRect_:(NSString*)aString { CGSize size; UIFont *nameFont=[UIFont fontWithName:@"Helvetica" size:13]; size=[aString sizeWithFont:nameFont constrainedToSize:CGSize

HTML5绘制空心的文本

1.设计源码 <!doctype html> <html> <head> <meta charset="utf-8"> <title>HTML5绘制空心的文本</title> <script type="text/javascript"> /** * 绘制空心的文本 */ function drawHollowText() { //找到<canvas>元素 var can

获取文本的宽高

/** * 获取文本的高度 * @param text * @return */ private int getTextHeight(String text){ Rect bounds = new Rect(); mFanPaint.getTextBounds(text,0,text.length(),bounds); int height = bounds.bottom + bounds.height(); return height; } /** * 获取文本的宽度 * @param tex

textarea文本域的高度随内容的变化而变化

用css控制textarea文本域的高度随内容的变化而变化,不出现滚动条. CSS代码: 复制代码 代码如下: .t_area{ width:300px; overflow-y:visible } <textarea class="t_area"> 随便在这里输入内容,textarea的高度会随着你输入的内容而变化,不会出现滚动条,实现很简单,就是一段css:overflow-y:visible </textarea> 首先,原则上实现textarea自适应必须

文本输入框自适应高度

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <HTML> <HEAD> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <TITLE&g

JavaScript获取文本框value

<html> <head> <title>获取文本框中的value</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <script language="javascript"> function getUsername(){ //思路:获取文本框节点:获取文本框节点的val

文本图片自适应高度小bug以及解决办法

自定义cell的文本图片自适应高度代码,如果存在自定义的cell赋值封装,就必须将自适应高度代码写在这个方法中 点击效果: 注:- (void)layoutSubviews 方法不能同时操作,否则会出现cell的高度错乱 显示: 点击:

JS动态生成Input文本框 并获取文本框值

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns= "http://www.w3.org/1999/xhtml" > <head> <meta http-equiv= "Conte

JS获取文本框值

一.获取文本框/编辑框/隐藏域框 <script type="text/javascript">function Mycheck(){  var checkstr="获取内容如下:\n";  if (document.form1.文章作者.value != ""){     checkstr+="作者名称:"+document.form1.文章作者.value+"\n";  }  if (doc