Label background vertical size changes depending on text. Are there "clean" ways to make label background size constant?


This image shows what I am trying to describe. If label text conatins letter “g”, then label vertical size changes. This leads to problems, when trying to align label with other elements.

Right now in order to fix it I create a second label below and replace last letter with “g”. It does the job, but I was just wondering if there is a better way to do it? Maybe use specific font or some kind of property, which I am not familiar with?

Hey @TempName, thanks for the question.

The logic for drawing the label background tries to clamp it exactly to the text by calculating the text glyph heights. This includes the decenders in some characters like the g. It’s not currently possible to control this behavior through the API.

That said, the “background padding” is something you can control and you might be able to achieve a similar effect by adding or removing the padding based on whether you detect a letter that has a decender like g,p,q etc.

Check out this sandcastle for an example of this in practice. The padding won’t be truly consistent but maybe it’s closer to what you want. Just note that the values you use are applied to both the top and bottom.

1 Like