FontMetrics-----Draw Text, get text height

Source: Internet
Author: User
Tags drawtext

When Canvas draws text, the coordinates of the position are calculated using the FontMetrics object.

1  Public Static classFontMetrics {2     /**3 * The maximum distance above the baseline for the tallest glyph in4 * The font at a given text size.5      */6      Public floattop;7     /**8 * The recommended distance above the baseline for singled spaced text.9      */Ten      Public floatascent; One     /** A * The recommended distance below the baseline for singled spaced text. -      */ -      Public floatdescent; the     /** - * The maximum distance below the baseline for the lowest glyph in - * The font at a given text size. -      */ +      Public floatBottom; -     /** + * The recommended additional space to add between lines of text. A      */ at      Public floatleading; -}

Its various baselines can be consulted as follows:

1 /**draw various lines of a FontMetrics object*/2 Mpaint.reset ();3 Mpaint.setcolor (color.white);4Mpaint.settextsize (80);5 //FontMetrics Object6FontMetrics FontMetrics =mpaint.getfontmetrics ();7String Text = "ABCDEFG";8 //calculate each of the coordinates9 floatTextWidth =mpaint.measuretext (text);Ten floatBaseX = 30; One floatBasey = 700; A floatTopy = Basey +Fontmetrics.top; - floatAscenty = Basey +fontmetrics.ascent; - floatDescenty = Basey +fontmetrics.descent; the floatbottomy = Basey +Fontmetrics.bottom; - //Draw Text - canvas.drawtext (text, BaseX, Basey, mpaint); - //Baseline Depiction + Mpaint.setcolor (color.red); -Canvas.drawline (BaseX, Basey, BaseX +textWidth, Basey, mpaint); +Mpaint.settextsize (20); ACanvas.drawtext ("base", BaseX +textWidth, Basey, mpaint); at //base Painting -Canvas.drawcircle (BaseX, Basey, 5, mpaint); - //topline Depiction - Mpaint.setcolor (color.ltgray); -Canvas.drawline (BaseX, topy, BaseX +textWidth, Topy, mpaint); -Canvas.drawtext ("Top", BaseX +textWidth, Topy, mpaint); in //Ascentline Depiction - Mpaint.setcolor (color.green); toCanvas.drawline (BaseX, Ascenty, BaseX +textWidth, Ascenty, mpaint); +Canvas.drawtext ("Ascent", BaseX + textWidth, Ascenty + 10, mpaint); - //Descentline Depiction the Mpaint.setcolor (color.yellow); *Canvas.drawline (BaseX, Descenty, BaseX +textWidth, Descenty, mpaint); $Canvas.drawtext ("Descent", BaseX +textWidth, Descenty, mpaint);Panax Notoginseng //Buttomline Depiction - Mpaint.setcolor (Color.magenta); theCanvas.drawline (BaseX, bottomy, BaseX +textWidth, bottomy, mpaint); +Canvas.drawtext ("Buttom", BaseX + textWidth, bottomy + ten, mpaint);

Believe that through the above procedures, can be a good understanding of topline,buttomline,baseline,ascentline,descentline.

In addition: There are two methods of the paint class

1 /**2 * Return The distance above (negative) the baseline (ascent) based on the3 * Current typeface and text size.4  *5  * @returnThe distance above (negative) the baseline (ascent) based on the6 * Current typeface and text size.7  */8  Public native floatascent ();9 Ten /** One * Return the distance below (positive) the baseline (descent) based on the A * Current typeface and text size. -  * -  * @returnThe distance below (positive) the baseline (descent) based on the * The current typeface and text size. -  */ -  Public native floatDescent ();

Ascent (): The distance above the baseline (height above baseline)

Descent (): The distance below the baseline (height below baseline)

So ascent () + descent () can be regarded as the height of the text.

So far, how to get the height and width of the text has been announced:

Get Height:mPaint.ascent () + mpaint.descent ()

Get Width:mPaint.measureText (text)

FontMetrics-----Draw Text, get text height

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.