0

I have a TextBlock in which I want to display 2 lines of text in different font sizes, I do not however want all of the wasted space above each line.

My XAML is as follows...

<TextBlock Grid.Column="0" Grid.Row="0" FontWeight="Bold" Foreground="Red" TextAlignment="Center">

    <Run Text="A" FontSize="144" FontWeight="Bold" />

    <LineBreak />

    <Run Text="Service" FontSize="18" />

</TextBlock>

And what I end up with is...

An image of what I actually get from my XAML

Whereas what I want is...

An image of what I am trying to acheive

Note all the wasted space above the "A", and between the "A" and the "Service" line (I have removed this in the second image - by manipulating the image).

I have tried various combinations of LineHeight, LineStackingStrategy, Margin and Padding property values against not only the TextBlock but also the Paragraph (using a Style) but nothing seems to remove the space.

Can anybody suggest how I should actually achieve this; I am sure it must be possible.

Thanks.

Martin Robins
  • 6,033
  • 10
  • 58
  • 95

2 Answers2

3

You can use LineStackingStrategy property set to BlockLineHeight and specify a LineHeight to get what you want.

<TextBlock Grid.Column="0" Grid.Row="0" FontWeight="Bold" Foreground="Red" TextAlignment="Center"
           LineHeight="15.25"
           LineStackingStrategy="BlockLineHeight">
    <Run Text="A" FontSize="144" FontWeight="Bold" />
    <LineBreak />
    <Run Text="Service" FontSize="18" />
</TextBlock>

Image with result

Community
  • 1
  • 1
  • Thanks for this, I eventually used this technique, but implemented using the answer [here](http://stackoverflow.com/questions/9785322/textblock-as-big-as-a-capital-letter-ignoring-font-ascender-descender#15876463). – Martin Robins Feb 29 '16 at 10:45
  • In a more general solution, where you don't know the `FontSize` of the `TextBlock` at compile time (but are using a consistent size), just bind `LineHeight` to the same value that you bind `FontSize` to: `` – mrfelis Feb 15 '19 at 18:09
  • Based on @mrfelis comment, I'm binding LineHeight to FontSize: `LineHeight="{Binding FontSize, RelativeSource={RelativeSource Mode=Self}}"`. – Vimes Apr 20 '20 at 17:16
0

I don't think this will exactly turn out the way you are searching but you can try.

Put the TextBlock into a DockPanel or a StackPanel as you prefer and then then one of these two in a ViewBox.

The ViewBox tries to maximize what it contains relatively to the resolution of the wpf window.

The reason to put the TextBlock in a StackPanel is because the ViewBox does not accept more than one child.

Dispensable Joe
  • 74
  • 1
  • 11