Minimize text block size

Currently, if I have a <TextBlock> element with a font size of 200, the whole element takes up much more space than necessary.

Wasted space

The blue outline (from Blend) shows the space the element occupies when half of it is required. In any case, can I eliminate this space so that my elements are not so spaced apart? I tried margin, padding, etc., but most methods don't seem to work.

Ideal

Ideally, I would like <TextBlock> occupy only the amount of space indicated by the red field

Edit:

With lower cased letters

In response to stijn7, some space is really reserved for other letters (g, p, q, etc.). However, there is still a lot of space at the top (1 is the highest character in the Segoe user interface). If trimming a text block is not possible, can I still resize it so that there is no wasted space?

+6
source share
2 answers

The problem is that the TextBlock has a height that includes ascender and descender for rendering characters with a range of heights. The character of your choice covers the x-height font, but not beyond. To achieve the effect you need, you will need an API for rendering a slightly lower level, I would suggest trying the FormattedText class:

Provides low-level control for drawing text in Windows Presentation Foundation (WPF).

This class has the MaxTextHeight property. I have not tried this, but there is a chance that he will provide you with the necessary information.

+1
source

TextBlock provides two properties LineHeight and LineStackingStrategy for manual height adjustment. Please note: you need to set LineStackingStrategy to "BlockLineHeight" for LineHeight to take effect.

 <TextBlock LineHeight="180" LineStackingStrategy="BlockLineHeight" Text="0" FontSize="200"/> 
+1
source

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


All Articles