0
votes

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.

2
Give Service negative margin doesn't work? eg: Margin="0 -4 0 0"Peter

2 Answers

3
votes

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

0
votes

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.