How can I get the height of a given line descriptor?

How can I get the height of a given line descriptor?

enter image description here

For instance,

  • abc should return 0.
  • abcl should return 0.
  • abcp should return the distance from the departure line to the baseline.
  • abclp should return the distance from the descnder line to the baseline.

The best I could release was

 private int getDecender(String string, Paint paint) { // Append "l", to ensure there is Ascender string = string + "l"; final String stringWithoutDecender = "l"; final Rect bounds = new Rect(); final Rect boundsForStringWithoutDecender = new Rect(); paint.getTextBounds(string, 0, string.length(), bounds); paint.getTextBounds(stringWithoutDecender, 0, stringWithoutDecender.length(), boundsForStringWithoutDecender); return bounds.height() - boundsForStringWithoutDecender.height(); } 

However, my code smell is that they are not good enough. Is there a better and faster way?

+6
source share
2 answers

Actually, I was looking for the same functionality. It turns out to be much simpler, you do not even need a separate function.

If you just call getTextBounds () on the given string, the returned bounding box will already have this information.

For instance:

 paint.getTextBounds(exampleString1 , 0, exampleString1.length(), bounds); if (bounds.bottom > 0) Log.i("Test", "String HAS descender"); else Log.i("Test", "String DOES NOT HAVE descender"); 

Just say that bounds.top tells you to ascend the line (it has a negative value, since the Y axis 0 indicates the baseline of the line), and bounds.bottom indicates the descent of the line (which may be 0 or a positive value for lines that have descent )

+3
source

You should see Paint.FontMetrics . The descent member will give you the "Recommended distance below the baseline for selected text at intervals."

-1
source

Source: https://habr.com/ru/post/906369/


All Articles