Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Minimize Size of Textblock

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

Wasted Space

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.

Ideal

Ideally I would want the <textblock> to take up only the amount of space indicated by the red box

Edit:

With Lower Cased Letters

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?

like image 325
Kiang Teng Avatar asked Jan 09 '12 08:01

Kiang Teng


2 Answers

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.

like image 68
ColinE Avatar answered Oct 01 '22 07:10

ColinE


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"/>
like image 21
terphi Avatar answered Oct 01 '22 05:10

terphi