Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”. This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.

1. How to measure the length and width of a piece of text

The Paint class provides a method for measuring width and height:

getTextBounds(String text, int start, int end, Rect bounds)

Returns a rectangular box containing the smallest rectangle of all characters, starting with (0,0) by default.

MeasureText (String text, int start, int end) :

Returns the width of text.

2. Question 1 where is the coordinate (x,y) specified by the drawText function

Many people would think that the coordinates specified are always in the upper left corner, but when drawing text, the coordinates specified are not in the upper left corner.

drawText(@NonNull String text, float x, float y, @NonNull Paint paint)

DrawText using canvas. drawText and draw two points at (x,y)

3. Use getTextBounds to get the width of the text, and measureText to get the width

Rect rect1 = new Rect();
mPaint.getTextBounds(textStr,0,textStr.length(),rect1);

float textwidth =  mPaint.measureText(textStr);
Copy the code

The width obtained with measureText is slightly larger than the width obtained by getTextBounds.

Why are the two measurement methods different?

MeasureText () adds some extra width to the left and right sides of the text. This extra width is called Glyph’s AdvanceX.

GetTextBounds () returns the minimum width required for the current text, which is the width of the entire text tangent rectangle, and the accuracy of the two functions is different.

4.FontMetrics

Each field in FontMetrics relates to baseline

  • Ascent is the distance from baseline to the top of the character
  • Descent is the distance below baseline to the lowest point of a character
  • Leading is the distance between descent on the previous line and ascent on the next. If this value is 0 on only one line, it is sometimes required to calculate font height.
  • Top is the value of the highest character to baseline, the maximum value of ascent
  • Bottom to the value of baseline, the maximum value of descent
Paint.FontMetrics fontMetrics = mPaint.getFontMetrics();
Copy the code

You can see that the baseline plus ascent is not immediately at the top of the letter because there are so many words and so many symbols at the top.

The height obtained by getTextBounds is inaccurate, and to maximize the height of the font, you should use the absolute value of Descent plus the absolute value of Ascent.

5. Get the width and height of text

Public static float getTextWidth(String text, float textSize){Paint Paint = new Paint(); paint.setTextSize(textSize); return paint.measureText(text); Public static float getTextHeight(float textSize){Paint Paint = new Paint(); paint.setTextSize(textSize); FontMetrics fm = paint.getFontMetrics(); return fm.descent - fm.ascent; }Copy the code