1

Text can be underlined like this

myPaint.setUnderlineText(true);

Is there a way to get the thickness of the underline in pixels?

I browsed through the Paint source code, but I didn't see anything.

Notes

  • I'm aware of the hidden underlineThickness property of TextPaint. Even if I were to access it through reflection, it is only a multiplier, not the actual thickness.
  • I'm not trying to change the thickness (as is asked in this question).
  • I'm making a custom vertical TextView completely from scratch in which I must implement my own underlining. If possible I would like to use the standard thickness. Otherwise I will probably choose some proportion of the text height (using (bottom - top) / 16 seems close).

Testing Nikola Despotoski's answer

This answer stated that

  • textBottom = top + textSize
  • underlineHeight = bottom - textBottom

I tested it using another answer and slightly modifying the app to include an underline below the text for visual purposes. (For that I used mTextPaint.setUnderlineText(true);)

enter image description here

The red lines are the FontMetrics top, baseline, and bottom. Rounding to the nearest int we get

  • top = -211
  • bottom = 54
  • textSize = 200

Using Nikola Despotoski's answer this would give

  • textBottom = (-211) + (200) = -11
  • underlineHeight = (54) - (-11) = 65

Since 65 is greater than the entire distance from the baseline to the bottom, this is much thicker than the actual underline. So unless I am misunderstanding the answer, it appears to be wrong.

Community
  • 1
  • 1
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
  • I guess it's part of the font you are using. Each font has (or has not) a regular, a bold, an italic and an underlined variant. Meaning that the glyphs are drawn differently from the other font variants. I don't think it is something you can control. Of course, other than drawing the text using the regular variant of the font and then adding your very own drawn line below that. – Phantômaxx Oct 06 '17 at 10:45
  • @ModularSynth, I'm pretty sure you can use `Paint` to draw an underline using any font. In any case, though, I'm not trying to control the font or how `Paint` works. Rather, I just want to know what the thickness of the underline line is, whether that information is stored in the font or generated by `Paint`. – Suragch Oct 06 '17 at 13:54
  • Well, if **you** generate it by drawing a line (as I suggested), **you** decide its thickness. Therefore, the problem would be solved. – Phantômaxx Oct 06 '17 at 15:21
  • @ModularSynth, This is what I plan to do. The reason I asked the question is because I would like to use the same thickness as the standard underline at any particular font size. – Suragch Oct 06 '17 at 23:50
  • Well, unfortunately, I guess it will be a trial and error task. Some empyrical measurement set on different font sizes, to determine a magical number to be used as a scaling factor. – Phantômaxx Oct 07 '17 at 07:14

1 Answers1

0

I'm taking the case of usual TextView, you should try to swap top and bottom with left and right for your case, hence height becomes width.

textBottom = top + textSize will give you the bottom of the letters, the size of the underline is underlineHeight = bottom - textBottom. As we know the top and textSize are given in px.

enter image description here

Nikola Despotoski
  • 49,966
  • 15
  • 119
  • 148