Currently, if I have a <TextBlock>
element with font size 200, the entire element takes up much more space than needed.
The blue outline (from Blend) shows the space the element takes up, when easily half of that is needed. Is there anyway I can eliminate that space such that my elements are not spaced out so much? I've tried margin, padding etc but most methods don't seem to work.
Ideally I would want the <textblock>
to take up only the amount of space indicated by the red box
Edit:
In response to stijn7, some space is indeed reserved for other letters (g,p,q etc). However there still is a lot of space at the top (1 is the highest character in Segoe UI). If clipping the textblock is not possible, is there anyway I can resize it such that there is no wasted space?
The problem here is that the TextBlock has the height incorporates the ascender and descender on order to render characters with a range of heights. The character you have chosen happens to span the font x-height, but not beyond. In order to achieve the effect you are after you will need a slightly lower level text rendering API, I would suggest trying the FormattedText class:
Provides low-level control for drawing text in Windows Presentation Foundation (WPF) applications.
This class has a MaxTextHeight property. I have not tried this, but there is a chance it will give you the information you require.
The TextBlock exposes two properties, LineHeight and LineStackingStrategy to manually set the height. Note that you have to set LineStackingStrategy to "BlockLineHeight" for the LineHeight to take effect.
<TextBlock LineHeight="180" LineStackingStrategy="BlockLineHeight" Text="0" FontSize="200"/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With