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 ofTextPaint
. 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);
)
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.